Java泛型的应用

一、泛型类

 1 package generics;
 2 /**
 3  * 泛型类,格式:public class 类名<泛型类型1, ...>
 4  * @author zhongfg
 5  * @date 2015-06-16
 6  * @param <T>
 7  */
 8 class Student<T> {
 9     
10     private T num;
11 
12     public T getNum() {
13         return num;
14     }
15 
16     public void setNum(T num) {
17         this.num = num;
18     }
19     
20 }
21 
22 public class GenericsClass {
23     public static void main(String[] args) {
24         
25         Student<String> s1 = new Student<String>();
26         s1.setNum("001");
27         String num1 = s1.getNum();
28         System.out.println(num1);
29         
30         System.out.println("----------------");
31         
32         Student<Integer> s2 = new Student<Integer>();
33         s2.setNum(2);
34         int num2 = s2.getNum();
35         System.out.println(num2);
36     }
37 }

 

二、泛型方法

 1 package generics;
 2 /**
 3  * 泛型方法,public <泛型类型> 返回值  方法名(泛型类型)
 4  * @author zhongfg
 5  * @date 2015-06-16
 6  */
 7 class Actor {
 8     
 9     public <T> void show(T t){
10         
11         System.out.println(t);
12     }
13     
14 }
15 
16 public class GenericsMethod {
17 
18     public static void main(String[] args) {
19         
20         Actor a = new Actor();
21         a.show("Angelababy");
22         a.show(26);
23     }
24 }

 

三、泛型接口

 1 package generics;
 2 /**
 3  * 泛型类,格式:public interface 接口名<泛型类型1, ...>
 4  * @author zhongfg
 5  * @date 2015-06-16
 6  * @param <T>
 7  */
 8 interface Person<T> {
 9 
10     public abstract void show(T t);
11 }
12 
13 class PersonImpl<T> implements Person<T> {
14 
15     @Override
16     public void show(T t) {
17         // TODO Auto-generated method stub
18         System.out.println(t);
19     }
20 }
21 
22 public class GenericsInterface {
23     public static void main(String[] args) {
24         
25         Person<String> p1 = new PersonImpl<String>();
26         p1.show("黄晓明");
27         
28         Person<Integer> p2 = new PersonImpl<Integer>();
29         p2.show(38);
30     }
31 }

 

四、泛型高级之通配符

 1 package generics;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 
 6 /**
 7  * 泛型高级(通配符)
 8  * ?:任意类型,如果没有明确指定,那么就是Object或者任意的Java类了
 9  * ? extends E:向下限定,E及其子类
10  * ? super E:向上限定,E及其父类
11  * @author zhongfg
12  * @date 2015-06-16
13  */
14 class Animal {
15     
16 }
17 
18 class Dog extends Animal {
19     
20 }
21 
22 class Cat extends Animal {
23     
24 }
25 
26 public class GenericsWildcard {
27     public static void main(String[] args) {
28         
29         //明确指明泛型的时候,前后必须一致
30         Collection<Object> c1 = new ArrayList<Object>();
31 //        Collection<Object> c2 = new ArrayList<Animal>(); 报错,前后类型不一致
32 //        Collection<Object> c3 = new ArrayList<Dog>(); 同上
33 //        Collection<Object> c4 = new ArrayList<Cat>(); 同上
34         
35         //?表示任意类型都是可以的
36         Collection<?> c5 = new ArrayList<Object>();
37         Collection<?> c6 = new ArrayList<Animal>();
38         Collection<?> c7 = new ArrayList<Dog>();
39         Collection<?> c8 = new ArrayList<Cat>();
40         
41         //? extends E:向下限定,E及其子类
42 //        Collection<? extends Animal> c9 = new ArrayList<Object>(); 报错,Object不是Animal子类
43         Collection<? extends Animal> c10 = new ArrayList<Animal>();
44         Collection<? extends Animal> c11 = new ArrayList<Dog>();
45         Collection<? extends Animal> c12 = new ArrayList<Cat>();
46         
47         //? super E:向上限定,E及其父类
48         Collection<? super Animal> c13 = new ArrayList<Object>(); 
49         Collection<? super Animal> c14 = new ArrayList<Animal>();
50 //        Collection<? super Animal> c15 = new ArrayList<Dog>(); 报错,Dog不是Animal及其以上的类型
51 //        Collection<? super Animal> c16 = new ArrayList<Cat>(); 同上
52     }
53 }

 

posted @ 2015-06-16 20:46  Self_improve  阅读(239)  评论(0编辑  收藏  举报