package genericAndCollection;import java.io.Serializable;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class TestGenerics {interface Comparable{int compareTo(T f);}static class Fruit implements Comparable{@Overridepublic int compareTo(Fruit f) {return 0;}}static class Apple extends Fruit {@Overridepublic int compareTo(Fruit apple){return 1;} }static class Orange extends Fruit{@Overridepublic int compareTo(Fruit orange){return 1;} }public static void main(String[] args) {//1: the difference between ? and T//?: means that any number of non-determined class type, it represents N//T: means that one non-determined class type, it represents one//2: generics are not implementing polymorphism List list = new ArrayList();//3:static type or apparent type can not write while using the extends//to determine the type edge (downwards)List<? extends Fruit> list1 = new ArrayList();list1.add(null);//otherwise will get compilation error//3.1: correct wayList list111 = new ArrayList();list111.add(new Apple());List<? extends Fruit> list11 = list111;for(Fruit f: list11){}//to do}--read only//4: static type or apparent type can not read while using the super // to determine the type edge (upwards)//here, Gabriel said we can not get(compilation error) is wrong, it can not //use get due to no element thereList<? super Fruit> list2 = new ArrayList();list2.get(0);//4.1: correct wayList list22 = new ArrayList();list22.add(new Fruit());List<? super Fruit> list222 = list22;list222.get(0);//5: the actual type can not use ?, because jvm will have to know which //actual type initiated, here ? extends Fruit is any number of type possible//edge to FruitList<? extends Fruit> list3 = new ArrayList<? extends Fruit>();//6: Array with generics, it has been implemented PolymorphismFruit[] fruit = new Apple[5];fruit[0] = new Apple();//no error here, because while it runs, javap shows //anewarray command in bytecode, it doesn't actually init //the instance of Fruit and Apple, it uses type [Fruit//7: force to cast while traverse using iteratorList list7 = new ArrayList();//it records the actual types to meta data which located in signature //if in method parameterlist7.add(new Apple());list7.add(new Orange());Iterator it = list7.iterator();while(it.hasNext()){System.out.println(it.next());}//7.1: try this--it shows if no generics here, any type can be added to this list//and it won't be forced to cast to Fruit, because it really didn't erase typeList list77 = new ArrayList();list77.add(new Object());list77.add(new Orange());Iterator it1 = list77.iterator();while(it1.hasNext()){System.out.println((Apple)it1.next());//please do not froce it to cast //will have java.lang.ClassCastException}//8: implementing comparable//see top//if you want to implements Comparable for both Apple and Orange//which means only Aplle can compare with apple, Orange can compare with Orange//we can remove the implements on Fruit and write it to both Apple and Orange//9:List xx = new ArrayList();xx.add(1);List<? extends Integer> xxx = xx;//this will not have error, because it won't check //whether the Integer can be extended.}public void f(T t){t.wait();//here we just can use the method of Object}}
泛型
最新推荐文章于 2024-09-03 09:32:52 发布