泛型类
1.泛型类在数据结构中的使用(java中的数据结构都采用了泛型的思路进行构造,所以一般都用泛型来调用数据结构)
1 ArrayList<Integer> list = new ArrayList<Integer>(); 2 list.add(78); 3 list.add(89); 4 list.add(99); 5 list.add(60); 6 7 for (Integer score: list) 8 { 9 System.out.println(score); 10 } 11 12 Iterator<Integer> it = list.iterator(); 13 while (it.hasNext()) 14 { 15 System.out.println(it.next()); 16 }
1 Map<String,Integer> map = new HashMap<String,Integer>(); 2 map.put("a", 1); 3 map.put("b", 2); 4 map.put("c", 4); 5 Set<Entry<String,Integer> > s = map.entrySet(); 6 Iterator<Entry<String,Integer> > it = s.iterator(); 7 while (it.hasNext()) 8 { 9 Entry<String,Integer> et = it.next(); 10 String key = et.getKey(); 11 Integer value = et.getValue(); 12 System.out.println(key + " " + value); 13 }
2.自定义泛型类
自定义泛型类即在类中含义泛型变量即为自定义泛型类
例如:
1 public class Order<T> { 2 String orderName; 3 int orderId; 4 //类的内部声明一个泛型类T,命名为orderT 5 T orderT; 6 7 public Order(){}; 8 9 public Order(String orderName,int orderId,T orderT) 10 { 11 this.orderName = orderName; 12 this.orderId = orderId; 13 this.orderT = orderT; 14 } 15 16 //以下的两个方法都不属于泛型方法,它们都是泛型类中的某个方法 17 public T GetOrderT() 18 { 19 return orderT; 20 } 21 22 public void SetOrderT(T orderT) 23 { 24 this.orderT = orderT; 25 } 26 27 @Override 28 public String toString() { 29 return "Order [orderName=" + orderName + ", orderId=" + orderId + ", orderT=" + orderT + "]"; 30 } 31 32 }
3.泛型函数
注意:泛型类中对泛型进行处理的函数不属于泛型函数
例子:
1 //泛型方法实例: 2 public static <E> E GetE (E object) 3 { 4 return object; 5 } 6 /* 7 * 所以泛型方法的本质是:引用了泛型类型,并以此作为返回值或者参数 8 */
4.有关泛型的子类继承问题:
1 //将会默认为<Object,Object>,所以a,b可以被设置为任意类型 2 class Son1 extends Father{ 3 public void Set() 4 { 5 a = 1; 6 b = 2; 7 } 8 } 9 10 //此时a只能为字符串,b只能为整型 11 class Son2 extends Father<String,Integer>{ 12 public void Set() 13 { 14 a = "1"; 15 b = 2; 16 } 17 }
1 //此时则将不确定的泛型转移到子类Son3来确定,即仍保留了这个泛型 2 class Son3<T1,T2> extends Father<T1,T2> { 3 4 } 5 6 //保留了部分泛型,即T1确定为Integer,T2留到子类中再进行确定 7 class Son4<T2> extends Father<Integer,T2> { 8 9 }
5.泛型在继承方面的特点
1 /* 泛型在继承方面的特点: 2 * 1.类A是类B的父类,但G<A>和G<B>并不具有子父类的关系 3 * 如果可以具有子父类关系,则A中就会混入B的数据,不符合泛型的要求 4 * 2.类A是类B的父类,A<T>和B<T>仍是子父类的关系 5 * 6 */
1 /* 通配符的使用 2 * 通配符:? 3 * 4 * 类A是类B的父类,G<A>和G<B>无关系,但二者的共同父类是G<?> 5 * 6 */
1 public static void test2() 2 { 3 List<Object> list1 = null; 4 List<String> list2 = null; 5 6 List<?> list = null; 7 8 list = list1; 9 list = list2; 10 11 List<String> list3 = new ArrayList<>(); 12 list3.add("aa"); 13 list3.add("bb"); 14 list3.add("cc"); 15 16 //对于List<?>的list,无法进行添加数据,只能添加null 17 list.add("123");//报错 18 list.add(null);//正常执行 19 20 //允许读取数据,读取数据的类型为Object 21 Object o = list.get(0); 22 System.out.println(o); 23 }
6.有限制条件的通配符使用
1 public void test4() 2 { 3 /* 有限制条件的通配符使用 4 * 若A是B的父类,则记为A > B 所以 Object > A > B 5 * ? extends A: 6 * 分三种情况进行分析: 7 * 1.涉及到G(某一数据结构)<? extends A>整体赋值时,满足<=的含义,即A,B可以被赋值,Object不可以被赋值 8 * 2.涉及到从数据结构获取值时,满足>=的含义,即可以给Object和A对象赋值,不可以给B对象赋值 9 * 3.向数据结构添加数据类型:无法向数据结构中添加除null外的任意数据类型 10 * 11 * ? super A: 12 * 分三种情况进行分析: 13 * 1.涉及到G(某一数据结构)<? super A>整体赋值时,满足>=的含义,即A,Object可以被赋值,B不可以被赋值 14 * 2.涉及到从数据结构获取值时,满足>的含义,只能给Object的对象赋值 15 * 3.向数据结构添加数据类型:只能向数据结构中添加A的数据类型 16 * 17 */ 18 List<? extends Person> list1 = null; 19 List<? super Person> list2 = null; 20 21 //情况1 22 List<Student> list3 = new ArrayList<>(); 23 List<Person> list4 = new ArrayList<>(); 24 List<Object> list5 = new ArrayList<>(); 25 26 list1 = list3; 27 list1 = list4; 28 list1 = list5;//该行会报错 29 30 list2 = list3;//该行会报错 31 list2 = list4; 32 list2 = list5; 33 34 //情况2 35 Object o1 = list1.get(0); 36 Person p1 = list1.get(0); 37 Student s1 = list1.get(0);//该行会报错,因为list1的类是Person类或者Person的父类 38 39 Object o2 = list2.get(0); 40 Person p2 = list2.get(0);//该行会报错 41 Student s3 = list2.get(0);//该行会报错 42 43 //情况3 44 list1.add(new Student());//编译不通过 45 list1.add(new Person());//编译不通过 46 list1.add(new Object()); 47 48 list2.add(new Student()); 49 list2.add(new Person()); 50 list2.add(new Object());//编译不通过 51 52 }