javaSE-泛型

泛型:
1,JDK1.5版本出现的新特性,安全机制。
2,泛型将运行时期的问题转移到了编译时期。
3,避免了强制转换的麻烦。
4,泛型技术是用于编译时期的技术。

泛型的体现:
<> 这就是用于定义类型参数的符号。
泛型可以简单的理解为,接收具体的元素类型。

对源码进行编译时,通过泛型进行类型的检查。
如果类型没有问题,则将源码编译成class文件。

注意,class文件中是不带有泛型信息的。这种情况称之为泛型擦除。

public static void main(String[] args) {

		// String[] arr = new String[3];//先明确类型。
		// arr[0] = new Integer(34);//存储时,如果类型不匹配,就编译报错。

		ArrayList al = new ArrayList();

		al.add("abcd"); // add(Object obj);
		al.add("cba"); // add(Object obj);
		al.add("cba"); // add(Object obj);
		// al.add(5);//自动装箱,一旦指定了具体元素的类型,存储时不匹配,就编译失败。

		for (Iterator it = al.iterator(); it.hasNext();) {
			// Object obj = it.next();
			String s = it.next();
			System.out.println(s.length());
		}
	}

在类上定义泛型

public class GenericDemo2 {

	public static void main(String[] args) {

		
		Person p = new Person();
		 
//		Tool tool = new Tool();
//		tool.setObject("abc");
//		Person pp = (Person)tool.getObject();
//		pp.getName();
		
		
//		Util u = new Util();
//		u.setObject("abc");
//		Person ppp = u.getObject();
	}

}

//该工具类只能操作Person。

//有了泛型以后,这个类就可以重新设计了。
class Util{//泛型类。较以前避免了强制麻烦,还提高了安全。
	
	private QQ obj;
	public void setObject(QQ obj){
		this.obj = obj;
	}
	public QQ getObject(){
		return obj;
	}
	
}


//可以不可以定义一个操作所有对象的工具类呢?可以,可以使用Object 
class Tool{
	private Object obj;
	public void setObject(Object obj){
		this.obj = obj;
	}
	public Object getObject(){
		return obj;
	}
}
/*
class Tool{
	
	private Person p;
	public void setPerson(Person p){
		this.p = p;
	}
	public Person getPerson(){
		return p;
	}
}
*/

class T {
	private T t;

	public T getT() {
		return t;
	}

	public void setT(T t) {
		this.t = t;
	}
	
	
}

定义在方法上的泛型


public class GenericDemo3 {

	public static void main(String[] args) {

		Demo3 d3 = new Demo3();
		d3.show("abc");
		d3.print("haha");
		d3.method("abcde");
		d3.method(new Integer(56));
		
		Demo3 d33 = new Demo3();
		d33.print(8);
//		d33.show("hehe");
		d33.method("hehe");
		
		
		
	}

}
/*
 * 类中操作的对象类型不确定,将泛型定义在类上。
 * 方法中操作的类型不确定,将泛型定义在方法上。
 * 
 * 如果是在静态方法上使用泛型,该泛型必须定义在方法上。
 */

class Demo3{
	
	public void show(W w){
		System.out.println("show :"+w);
	}
	
	public void print(W w){
		System.out.println("print:"+w);
	}
	/*
	 * 将泛型定义在方法上。
	 */
	public  void method(M m){
		System.out.println("method:"+m);
	}
	
	public static  void function(K w){
		System.out.println("function:"+w);
	}
	
}

泛型接口

public class GenericDemo4 {
	public static void main(String[] args) {

		// 1,
		Demo d = new Demo();
		d.show("haha");

		// 2
		Demo2 d2 = new Demo2();
		d2.show("haha");
	}

}

/*
 * 泛型接口。
 */

interface Inter {
	public void show(U u);
}

class Demo2 implements Inter {

	@Override
	public void show(T u) {
	}

}

class Demo implements Inter {

	@Override
	public void show(String u) {
	}
}

泛型的通配符

public class GenericTest {

	public static void main(String[] args) {

		ArrayList al = new ArrayList();
		
		al.add("abc1");
		al.add("abc2");
		al.add("abc3");
		
		ArrayList al2 = new ArrayList();
		
		al2.add(45);
		al2.add(67);
		al2.add(22);
		
		printCollection(al);
		printCollection(al2);
		
		
	}
	public static  void printCollection(Collection coll){
		for(Iterator it = coll.iterator(); it.hasNext(); ){
			System.out.println(it.next().toString());
		}
	}
	
	
	/**
	 * 建立一个专门用打印集合元素的方法。
	 */
//	public static  void printCollection(Collection coll){
//		for(Iterator it = coll.iterator(); it.hasNext(); ){
//			T t = it.next();
//			System.out.println(t.toString());
//		}
//	}
	
	
	

}

? 未知  只能调用object类的方法  本身不能当作具体的对象使用 则可以

泛型的限定

public class GenericDemo5 {

	public static void main(String[] args) {

		/*
		 * 泛型的限定。	
		 * 
		 * ? extends E :接收E类型或者E的子类型。
		 * 
		 * ? super E : 接收E类型或者E的父类型。
		 * 
		 * 	
		 */
		ArrayList al = new ArrayList();
		al.add(new Person("lisi1",21));
		al.add(new Person("lisi2",22));
		al.add(new Person("lisi3",23));
		
		show(al);
		
		ArrayList al2 = new ArrayList();
		al2.add(new Student("xiaoming1",21));
		al2.add(new Student("xiaoming2",22));
		al2.add(new Student("xiaoming3",23));
//		show(al2);
		
		ArrayList al3 = new ArrayList();
		al3.add("abc1");
		al3.add("abc2");
		al3.add("abc3");
//		show(al3);
		ArrayList al4 = new ArrayList();
		al4.add(new Worker("dachui1",21));
		al4.add(new Worker("dachui1",22));
		al4.add(new Worker("dachui1",23));
		show(al4);
		
	}
	
	public static void show(Collection coll){
		
		for (Iterator it = coll.iterator(); it.hasNext();) {
			
			System.out.println(it.next());
			
		}
		
	}

}

举例

public class GenericTest2 {
	public static void main(String[] args) {

		/*
		 * 了解api中的泛型的体现。
		 * boolean containsAll(Collection c) 
		 */
		
		ArrayList a1 = new ArrayList();
		a1.add("abc1");
		a1.add("abc2");
		a1.add("abc3");
		
		ArrayList a2 = new ArrayList();
		a2.add("abc1");
		a2.add("abc2");
		
		ArrayList a3 = new ArrayList();
		a3.add(45);
		a3.add(89);
		
		boolean b = a1.containsAll(a3);
		System.out.println("b="+b);
		
		
		
		
		/*
		 * 了解api中的泛型上限。 ? extends E
		 * 
		 * boolean addAll(Collection c) 
		 * 
		 */
		
		ArrayList a4 = new ArrayList();
		a4.add(new Person("lisi1",21));
		a4.add(new Person("lisi2",22));
		a4.add(new Person("lisi3",23));
		
		
		ArrayList a5 = new ArrayList();
		a5.add(new Student("小明4",24));
		a5.add(new Student("小明5",25));
		a5.add(new Student("小明6",26));
		
		a4.addAll(a5);
		
		Iterator it = a4.iterator();
		while(it.hasNext()){
			Person p = it.next();
			System.out.println(p.getName()+":"+p.getAge());
		}
	}

}


interface MyCollection{
	
	boolean myContainsAll(MyCollection c);
	
	boolean addAll(MyCollection c);
}

public class GenericTest3 {

	public static void main(String[] args) {

		/*
		 * 了解下限的api体现。
		 * 
		 * TreeSet(Comparator comparator)
		 */
		
		
		TreeSet ts = new TreeSet(new ComparatorByAge());
		ts.add(new Student("xiaoming3",23));
		ts.add(new Student("xiaoming8",21));
		ts.add(new Student("xiaoming2",22));
		ts.add(new Student("xiaoming9",20));
		
		
		
		Iterator it = ts.iterator();
		while(it.hasNext()){
			Student s = it.next();
			System.out.println(s);
		}
		
		
		TreeSet ts2 = new TreeSet(new ComparatorByAge());
		ts2.add(new Worker("dachui2",23));
		ts2.add(new Worker("dachui9",21));
		ts2.add(new Worker("dachui0",22));
		ts2.add(new Worker("dachui6",20));
		Iterator it2 = ts2.iterator();
		while(it2.hasNext()){
			Worker s = it2.next();
			System.out.println(s);
		}
	}

}
class ComparatorByAge implements Comparator{

	@Override
	public int compare(Person o1, Person o2) {
		
		int temp = o1.getAge() - o2.getAge();
		
		return temp==0?o1.getName().compareTo(o2.getName()):temp;
	}
}


class MyTreeSet{
	MyTreeSet(Comparator comp){
		
	}
}

泛型的误区解释

public void test() {
		List list1 = new ArrayList();
		List list2 = new ArrayList();
		List list3 = new ArrayList();
		List list4 = new ArrayList();
		list1.add(new Object());//obj
		list2.add(new String("string"));//String
		list3.add(new Object());//obj
		list4.add(new String ("name"));//String
	}


public class GenericTest4 {

	public static void main(String[] args) {

//		ArrayList al1 = new ArrayList();//错的
//		al1.add(new Dog());
//		al1.add(new Cat());
		
		
//		ArrayList al2 = new ArrayList();
		//只要左右两边一致,这是最简单的思考方式。
		
	}
//	public static void show(Collection coll){
//		coll.add("haha");
//		coll.add(4);
//	}
}


你可能感兴趣的:(javaSE)