泛型即参数化类型,该类中定有一个参数化类型的属性.当成普通属性使用即可。
https://www.cnblogs.com/lwbqqyumidi/p/3837629.html
定义了一个List类型的集合,先向其中加入了两个字符串类型的值,随后加入一个Integer类型的值。这是完全允许的,因为此时list默认的类型为Object类型。在之后的循环中,由于忘记了之前在list中也加入了Integer类型的值或其他编码原因,很容易出现类似于//1中的错误。因为编译阶段正常,而运行时会出现“java.lang.ClassCastException”异常。因此,导致此类错误编码过程中不易发现。
1 public class GenericTest {
2
3 public static void main(String[] args) {
4 List list = new ArrayList();
5 list.add("qqyumidi");
6 list.add("corn");
7 list.add(100);
8
9 for (int i = 0; i < list.size(); i++) {
10 String name = (String) list.get(i); // 1
11 System.out.println("name:" + name);
12 }
13 }
14 }
在如上的编码过程中,我们发现主要存在两个问题:
1.当我们将一个对象放入集合中,集合不会记住此对象的类型,当再次从集合中取出此对象时,改对象的编译类型变成了Object类型,但其运行时类型任然为其本身类型。
2.因此,//1处取出集合元素时需要人为的强制类型转化到具体的目标类型,且很容易出现“java.lang.ClassCastException”异常。
那么有没有什么办法可以使集合能够记住集合内元素各类型,且能够达到只要编译时不出现问题,运行时就不会出现“java.lang.ClassCastException”异常呢?答案就是使用泛型。
泛型,即“参数化类型”。一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参。那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传入具体的类型(类型实参)。
public class GenericTest {
2
3 public static void main(String[] args) {
4 /*
5 List list = new ArrayList();
6 list.add("qqyumidi");
7 list.add("corn");
8 list.add(100);
9 */
10
11 List<String> list = new ArrayList<String>();
12 list.add("qqyumidi");
13 list.add("corn");
14 //list.add(100); // 1 提示编译错误
15
16 for (int i = 0; i < list.size(); i++) {
17 String name = list.get(i); // 2
18 System.out.println("name:" + name);
19 }
20 }
21 }
采用泛型写法后,在//1处想加入一个Integer类型的对象时会出现编译错误,通过List<String>,直接限定了list集合中只能含有String类型的元素,从而在//2处无须进行强制类型转换,因为此时,集合能够记住元素的类型信息,编译器已经能够确认它是String类型了。
<T>和<?>的区别?
T表示自定义参数化类型。?表示通配符参数化类型,用于多次创建某个类时取的时候不用区分参数化类型。
public static void main(String[] args) { 4 5 Box<Number> name = new Box<Number>(99); 6 Box<Integer> age = new Box<Integer>(712); 7 8 getData(name); 9 10 //The method getData(Box<Number>) in the type GenericTest is 11 //not applicable for the arguments (Box<Integer>) 12 getData(age); // 1 13 14 } 15 16 public static void getData(Box<Number> data){ 17 System.out.println("data :" + data.getData()); 18 }
我们发现,在代码//1处出现了错误提示信息:The method getData(Box<Number>) in the t ype GenericTest is not applicable for the arguments (Box<Integer>)。显然,通过提示信息,我们知道Box<Number>在逻辑上不能视为Box<Integer>的父类。那么,原因何在呢?
1 public class GenericTest { 2 3 public static void main(String[] args) { 4 5 Box<Integer> a = new Box<Integer>(712); 6 Box<Number> b = a; // 1 7 Box<Float> f = new Box<Float>(3.14f); 8 b.setData(f); // 2 9 10 } 11 12 public static void getData(Box<Number> data) { 13 System.out.println("data :" + data.getData()); 14 } 15 16 } 17 18 class Box<T> { 19 20 private T data; 21 22 public Box() { 23 24 } 25 26 public Box(T data) { 27 setData(data); 28 } 29 30 public T getData() { 31 return data; 32 } 33 34 public void setData(T data) { 35 this.data = data; 36 } 37 38 }
这个例子中,显然//1和//2处肯定会出现错误提示的。在此我们可以使用反证法来进行说明。
假设Box<Number>在逻辑上可以视为Box<Integer>的父类,那么//1和//2处将不会有错误提示了,那么问题就出来了,通过getData()方法取出数据时到底是什么类型呢?Integer? Float? 还是Number?且由于在编程过程中的顺序不可控性,导致在必要的时候必须要进行类型判断,且进行强制类型转换。显然,这与泛型的理念矛盾,因此,在逻辑上Box<Number>不能视为Box<Integer>的父类。
好,那我们回过头来继续看“类型通配符”中的第一个例子,我们知道其具体的错误提示的深层次原因了。那么如何解决呢?总部能再定义一个新的函数吧。这和Java中的多态理念显然是违背的,因此,我们需要一个在逻辑上可以用来表示同时是Box<Integer>和Box<Number>的父类的一个引用类型,由此,类型通配符应运而生。
类型通配符一般是使用 ? 代替具体的类型实参。注意了,此处是类型实参,而不是类型形参!且Box<?>在逻辑上是Box<Integer>、Box<Number>...等所有Box<具体类型实参>的父类。由此,我们依然可以定义泛型方法,来完成此类需求。
public static void main(String[] args) { 4 5 Box<String> name = new Box<String>("corn"); 6 Box<Integer> age = new Box<Integer>(712); 7 Box<Number> number = new Box<Number>(314); 8 9 getData(name); 10 getData(age); 11 getData(number); 12 } 13 14 public static void getData(Box<?> data) { 15 System.out.println("data :" + data.getData()); 16 }