泛型

1.泛型类定义以及使用

 1 public class People<T> {
 2     private T info;
 3     
 4     public People(){
 5         
 6     }
 7     
 8     public People(T info){
 9         this.info=info;
10     }
11     
12     public void setInfo(T info){
13         this.info=info;
14     }
15     
16     public T getInfo(){
17         return this.info;
18     }
19     
20     public static void main(String[] args){
21         String name = "zhang";
22         int age = 18;
23         System.out.println(new People(name).getInfo());
24         
25         System.out.println(new People(age).getInfo());
26     }
27 }
View Code

执行结果:

zhang
18

从代码中可以看到,对于info,我们可以传入String或者int类型的值。

 

2. 泛型类的子类定义以及使用

 1 public class Student extends People<String>{
 2     
 3     public String getInfo(){
 4         return "sub" +"    " +super.getInfo();
 5     }
 6 
 7     public static void main(String[] args) {
 8         Student stu = new Student();
 9         stu.setInfo("student");
10         System.out.println(stu.getInfo());
11     }
12 }
View Code

从代码中可以看到,在继承的时候就设定好了String,这里尽量设定,如果不设定编译时会报警告。

 

3.类型通配符以及类型通配符上限

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 public class TypeWildcard {
 5 
 6     public void test(List<?> c) {
 7         for (int i = 0; i < c.size(); i++) {
 8             System.out.println(c.get(i));
 9         }
10     }
11 
12     public void test2(List<? extends String> s) {
13         for (int i = 0; i < s.size(); i++) {
14             System.out.println(s.get(i));
15         }
16     }
17 
18     public static void main(String[] args) {
19 
20         TypeWildcard tw = new TypeWildcard();
21 
22         List<String> stringList = new ArrayList<>();
23         stringList.add("String1");
24         stringList.add("String2");
25 
26         List<Integer> intList = new ArrayList<>();
27         intList.add(3);
28         intList.add(4);
29 
30         tw.test(stringList);
31         tw.test(intList);
32 
33         List<String> stringList2 = new ArrayList<>();
34         stringList2.add("String5");
35         stringList2.add("String6");
36 
37         // List<Integer> intList2= new ArrayList<>();
38         // intList2.add(7);
39         // intList2.add(8);
40 
41         tw.test2(stringList2);
42         // tw.test2(intList);
43 
44     }
45 
46 }
View Code

如代码中,如果test2方法中传入List<int>类型值,就会报下图的错误。以此来达到限制的作用。

 

4.泛型方法

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 import java.util.List;
 4 
 5 public class GenericMethodTest {
 6     
 7     public <T> void fromArrayToCollection(T[] a, Collection<T> c){
 8         for(T o : a){
 9             c.add( (T) a);
10         }
11     }
12     
13     public <E> void test(Collection<? extends E> from ,Collection<E> to ){
14         //这里的方式表明,?是E或者E的子类,即为类型统配符设定了上限
15         for(E ele : from){
16             to.add( (E) ele);
17         }
18     }
19     
20     public <E> void test2(Collection<E> from ,Collection<E> to ){
21         for(E ele : from){
22             to.add( (E) ele);
23         }
24     }
25     
26     public <E,H> void test3(List<H> from ,Collection<E> to ){
27         //这里的方式表明,类型形参可以同时声明多个
28     }
29     
30     public <E> void test4(Collection<? super E> from ,Collection<E> to ){
31         //这里的方式表明,?是E或者E的父类,即为类型统配符设定了下限
32     }
33 
34     public static void main(String[] args) {
35         GenericMethodTest gmt = new GenericMethodTest();
36         
37         //这里T就是Object
38         Object[] oa = new Object[100];
39         Collection<Object> co =new ArrayList<>();
40         gmt.fromArrayToCollection(oa, co);
41         
42         
43         //这里T就是String
44         String[] sa = new String[100];
45         Collection<String> cs =new ArrayList<>();
46         gmt.fromArrayToCollection(sa, cs);
47         //这里T就是Object
48         gmt.fromArrayToCollection(sa, co);
49         
50         
51         Integer[] ia=new Integer[100];
52         Float[] fa = new Float[100];
53         Number[] na = new Number[100];
54         Collection<Number> cn = new ArrayList<>();
55         //这里T就是Number
56         gmt.fromArrayToCollection(ia, cn);
57         //这里T就是Number
58         gmt.fromArrayToCollection(fa, cn);
59         //这里T就是Number
60         gmt.fromArrayToCollection(na, cn);
61         //这里T就是Object
62         gmt.fromArrayToCollection(na, co);
63         //下面T代表String,但是na是一个Number数组,因为Number既不是String类型,也不是String的子类,所以报错
64         //gmt.fromArrayToCollection(na, cs);
65         
66     //====================================================================================    
67 
68         //上面内容演示了java自动识别类型
69         //有时候防止自动识别错误,需要为设定一个上限,如test方法
70         List<Object> ao = new ArrayList<>();
71         List<String> as = new ArrayList<>();
72         //这里string就是extends object
73         gmt.test(as, ao);
74         //下面会报错,因为这会让自动识别产生误解
75         //gmt.test2(as, ao);
76         
77     }
78 
79 }
posted @ 2016-12-24 18:20  月色深潭  阅读(260)  评论(0编辑  收藏  举报