Joyen.fu
日中则昃,月盈则食。

泛型

JDK1.5之后出现的新技术
泛型最大的特点是类中的属性类型可以由外部决定

 1 package com.fwj.genericsdemo;
 2 
 3 public class Point<T> {
 4 
 5     private T x;
 6     private T y;
 7     
 8     public T getX() {
 9         return x;
10     }
11     public void setX(T x) {
12         this.x = x;
13     }
14     public T getY() {
15         return y;
16     }
17     public void setY(T y) {
18         this.y = y;
19     }
20     
21 }

程序加入泛型后,我们发现属性的操作类型不再由程序固定设置,而是在实例化时的外部决定。

 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point<Integer> p = new Point<Integer>();
 7         p.setX(20);
 8         p.setY(30);
 9         System.out.println(p.getX()+" "+p.getY());
10     }
11 
12 }

运行结果:

20 30

在构造方法上使用泛型

 1 package com.fwj.genericsdemo;
 2 
 3 public class Point<T> {
 4 
 5     private T x;
 6     private T y;
 7     
 8     public Point() {
 9         super();
10     }
11     public Point(T x, T y) {
12         super();
13         this.x = x;
14         this.y = y;
15     }
16     public T getX() {
17         return x;
18     }
19     public void setX(T x) {
20         this.x = x;
21     }
22     public T getY() {
23         return y;
24     }
25     public void setY(T y) {
26         this.y = y;
27     }
28     
29 }
 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point<Integer> p = new Point<Integer>(20,30);
 7         
 8         System.out.println(p.getX()+" "+p.getY());
 9     }
10 
11 }

运行结果:

20 30

擦除泛型

如果没有指定泛型,则表示擦除泛型。泛型擦除后,将按照Object接收,以保证程序不出现问题。

 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point p = new Point(20,30);
 7         int x = (Integer)p.getX();//需要向下转型
 8         int y = (Integer)p.getY();
 9         System.out.println(x+" "+y);
10     }
11 
12 }

通配符?

 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point<Object> p = new Point<Object>(20,30);
 7         Point<Integer> p1 = new Point<Integer>(20,30);
 8         func(p);
 9         func(p1);
10     }
11     
12     public static void func(Point<?> p){
13         System.out.println(p.getX()+" "+p.getY());
14     }
15 }

程序中的“?”表示可以接收任意的泛型类型,但只是接受输出,并不能修改。

泛型上限

上限是指一个操作泛型最大的操作父类,例如上限设置为“Number”类型,那么此时所能接受的类型只能是Number及其子类,

语法如下:

?extends 类

 1 package com.fwj.genericsdemo;
 2 
 3 public class Point<T extends Number> {//设置泛型上限是Number
 4 
 5     private T x;
 6     private T y;
 7     
 8     public Point() {
 9         super();
10     }
11     public Point(T x, T y) {
12         super();
13         this.x = x;
14         this.y = y;
15     }
16     public T getX() {
17         return x;
18     }
19     public void setX(T x) {
20         this.x = x;
21     }
22     public T getY() {
23         return y;
24     }
25     public void setY(T y) {
26         this.y = y;
27     }
28     
29 }
1 package com.fwj.genericsdemo;
2 
3 public class GenericsDemo {
4 
5     public static void main(String[] args) {
6         Point<Integer> p = new Point<Integer>(20,30);
7         System.out.println(p.getX()+" "+p.getY());
8     }
9 }
 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point<Integer> p1 = new Point<Integer>(20,30);
 7         func(p1);
 8     }
 9     
10     public static void func(Point<? extends Number> p){//表示可以接受Number的子类
11         System.out.println(p.getX()+" "+p.getY());
12     }
13 }

泛型下限

泛型的下限指的是只能设置其具体的类或者父类,语法如下:

? super 类

 1 package com.fwj.genericsdemo;
 2 
 3 public class Point<T> {
 4 
 5     private T x;
 6     private T y;
 7     
 8     public Point() {
 9         super();
10     }
11     public Point(T x, T y) {
12         super();
13         this.x = x;
14         this.y = y;
15     }
16     public T getX() {
17         return x;
18     }
19     public void setX(T x) {
20         this.x = x;
21     }
22     public T getY() {
23         return y;
24     }
25     public void setY(T y) {
26         this.y = y;
27     }
28     
29 }
 1 package com.fwj.genericsdemo;
 2 
 3 public class GenericsDemo {
 4 
 5     public static void main(String[] args) {
 6         Point<String> p1 = new Point<String>("heh","hah");
 7         func(p1);
 8     }
 9     
10     public static void func(Point<? super String> p){//泛型下限为String
11         System.out.println(p.getX()+" "+p.getY());
12     }
13 }

 

posted on 2013-07-25 16:57  Joyen.fu  阅读(210)  评论(0编辑  收藏  举报