`
jiao13953900900
  • 浏览: 32234 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

黑马程序员-泛型

    博客分类:
  • java
阅读更多

 

---------------------- android培训java培训、期待与您交流! ----------------------

 

 

泛型

 

 

 

泛型:

JDK1.5版本以后出现新特性。用于解决安全问题,是一个类型安全机制。

 

好处

1.将运行时期出现问题ClassCastException,转移到了编译时期。,

方便于程序员解决问题。让运行时问题减少,安全。,

 

2,避免了强制转换麻烦。

 

 

泛型格式:通过<>来定义要操作的引用数据类型。

 

在使用java提供的对象时,什么时候写泛型呢?

 

通常在集合框架中很常见,

只要见到<>就要定义泛型。

 

其实<> 就是用来接收类型的。

 

当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。

import java.util.*;
class GenericDemo 
{
	public static void main(String[] args) 
	{
		//使用泛型进行限定元素类型
		ArrayList<String> al = new ArrayList<String>();

		al.add("abc01");//被限定以后,只能添加与限定类型相同类型的元素
		al.add("abc0991");
		al.add("abc014");

		//al.add(4);
		////al.add(new Integer(4));//如果添加的元素类型不正确会编译失败
		

		Iterator<String> it = al.iterator();
		while(it.hasNext())
		{
			String s = it.next();

			System.out.println(s+":"+s.length());
		}
	}
}

 

 

//在使用接口是时候指定类型
class LenComparator implements Comparator<String>
{
	//在接口上指定以后,内部使用的都是相同的类型
	public int compare(String o1,String o2)
	{
		int num = new Integer(o2.length()).compareTo(new Integer(o1.length()));

		if(num==0)
			return o2.compareTo(o1);
		return num;
	}
}

 

 

泛型类。

什么时候定义泛型类?

当类中要操作的引用数据类型不确定的时候,

早期定义Object来完成扩展。

现在定义泛型来完成扩展。

 

//泛型前做法。
class Tool
{
	private Object obj;
	public void setObject(Object obj)
	{
		this.obj = obj;
	}
	public Object getObject()
	{
		return obj;
	}
}

//在有了泛型以后,将泛型定义到类上
class Utils<QQ>
{
	private QQ q;
	public void setObject(QQ q)//类中的方法可以使用泛型进行定义
	{
		this.q = q;
	}
	public QQ getObject()
	{
		return q;
	}
}


class  GenericDemo3
{
	public static void main(String[] args) 
	{
		//在创建该类对象的时候指定类型
		Utils<Worker> u = new Utils<Worker>();

		u.setObject(new Student());
		Worker w = u.getObject();

		/*按照以前的做法,要处理任意类型的时候,使用到某个类型特有的 方法,需要进行强转。
		Tool t = new Tool();
		t.setObject(new Student());
		Worker w = (Worker)t.getObject();
		*/
	}
}

 

 

 

 

泛型类定义的泛型,在整个类中有效。如果被方法使用,

那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。

 

为了让不同方法可以操作不同类型,而且类型还不确定。

那么可以将泛型定义在方法上。

 

 

 

特殊之处:

静态方法不可以访问类上定义的泛型。

如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。

 

//泛型定义到类上
class Demo<T>
{
	public void show(T t)
	{
		System.out.println("show:"+t);
	}
	public void print(T t)
	{
		System.out.println("show:"+t);
	}

}
class Demo<T>
{
	//泛型定义到方法上,使用本类的泛型进行定义
	public  void show(T t)
	{
		System.out.println("show:"+t);
	}
	//将泛型定义到方法上,可以使用与本类不同的泛型定义
	public <Q> void print(Q q)
	{
		System.out.println("print:"+q);
	}
	//静态方法不能使用类上定义的泛型,静态方法必须把泛型定义到方法上。
	public  static <W> void method(W t)
	{
		System.out.println("method:"+t);
	}
}
class GenericDemo4 
{
	public static void main(String[] args) 
	{
		Demo <String> d = new Demo<String>();
		d.show("haha");
		//d.show(4);//使用与泛型类不同类型的参数传递给泛型方法会编译失败
		d.print(5);
		d.print("hehe");

		Demo.method("hahahahha");


	}
}

 

//泛型定义在接口上。
interface Inter<T>
{
	void show(T t);
}
//在使用接口时指定泛型
class InterImpl implements Inter<String>
{
	public void show(String t)
	{
		System.out.println("show :"+t);
	}
}
//在实现一个接口的时候没有指定泛型类型,并把这个类定义为泛型类
class InterImpl<T> implements Inter<T>
{
	public void show(T t)
	{
		System.out.println("show :"+t);
	}
}
class GenericDemo5 
{
	public static void main(String[] args) 
	{
		//在创建这个类的对象的时候进行指定类型
		InterImpl<Integer> i = new InterImpl<Integer>();
		i.show(4);
		//InterImpl i = new InterImpl();
		//i.show("haha");
	}
}

 

 

? 通配符。也可以理解为占位符。

泛型的限定;

? extends E: 可以接收E类型或者E的子类型。上限。

? super E: 可以接收E类型或者E的父类型。下限

 

import java.util.*;
class  GenericDemo6
{
	public static void main(String[] args) 
	{
	

		ArrayList<Person> al = new ArrayList<Person>();
		al.add(new Person("abc1"));
		al.add(new Person("abc2"));
		al.add(new Person("abc3"));
		//printColl(al);

		ArrayList<Student> al1 = new ArrayList<Student>();
		al1.add(new Student("abc--1"));
		al1.add(new Student("abc--2"));
		al1.add(new Student("abc--3"));
		printColl(al1); 

	}
	//使用泛型限定方式,参数意为任意继承自Person的类型
	public static void printColl(Collection<? extends Person> al)
	{
		Iterator<? extends Person> it = al.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next().getName());
		}
	}
	
	public static void printColl(ArrayList<?> al)//ArrayList al = new ArrayList<Integer>();error
	{
		Iterator<?> it = al.iterator();


		while(it.hasNext())
		{
			System.out.println(it.next().toString());
		}
	}
	
}

class Person
{
	private String name;
	Person(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
}
//写一个类继承Person类
class Student extends Person
{
	Student(String name)
	{
		super(name);
	}
}

 

 

 

---------------------- android培训java培训、期待与您交流! ----------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics