Java之泛型编程

来源:互联网 发布:淘宝如何设置选项卡 编辑:程序博客网 时间:2024/05/20 18:01
1.概念

  泛型就是参数化类型。泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,提高代码的重用率。

2.案例

1)先看下面案例:

  1. //不适用泛型编程  
  2. Apple app0=new Apple();  
  3. Apple app1=new Apple();  
  4.    
  5. List li = new ArrayList();  
  6. li.add(app0);        //添加非需要类型时发现不了错误。  
  7. li.add(app1);  
  8.    
  9. Apple appUsed=(Apple)li.get(0);  
  10.    
  11. //使用泛型编程如下  
  12. Apple app0=new Apple();  
  13. Apple app1=new Apple();  
  14.    
  15. List<Apple> li = new ArrayList<Apple>();  
  16. li.add(app0);        //如果添加的对象类型错误,编译器即可发现。指定容器要持有的对象类型,用编译器来保证类型的正确性。  
  17. li.add(app1);  
  18.    
  19. Apple appUsed=li.get(0);  


 

  使用泛型的优点:大型应用时能显著降低程序的复杂度;泛型为较大的优化带来可能: 可以在编译期发现该类错误,而且在取出元素时不需要再进行类型判断,从而提高了程序的运行时效率。 

2)泛型类

有两个类如下,要构造两个类的对象,并打印出各自的成员x。

  1. public class StringFoo {  
  2.   
  3.     private String x;  
  4.   
  5.     public StringFoo(String x) {  
  6.              this.x = x;  
  7.     }  
  8.   
  9.   
  10.     public String getX() {  
  11.              return x;  
  12.     }  
  13.   
  14.      public void setX(String x) {  
  15.              this.x = x;  
  16.     }  
  17.  }  
  18.   
  19.  public class DoubleFoo {  
  20.   
  21.     private Double x;  
  22.   
  23.     public DoubleFoo(Double x) {  
  24.              this.x = x;  
  25.     }  
  26.   
  27.     public Double getX() {  
  28.              return x;  
  29.     }  
  30.   
  31.      public void setX(Double x) {  
  32.              this.x = x;  
  33.     }  
  34.  }  


 

用泛型来实现

  1. public class GenericsFoo<T> {  
  2.   
  3.   private T x;  
  4.   
  5.   public GenericsFoo(T x) {  
  6.      this.x = x;  
  7.   }  
  8.   
  9.    public T getX() {  
  10.      return x;  
  11.   }  
  12.   
  13.   public void setX(T x) {  
  14.      this.x = x;  
  15.   }  
  16. }  
  17.   
  18.    
  19. 代码实现:  
  20.   
  21.  public class GenericsFooDemo {  
  22.   
  23.   public static void main(String args[]){  
  24.   
  25.      GenericsFoo<String> strFoo=new GenericsFoo<String>("Hello Generics!");  
  26.      GenericsFoo<Double> douFoo=new GenericsFoo<Double>(new Double("33"));  
  27.      GenericsFoo<Object> objFoo=new GenericsFoo<Object>(new Object());  
  28.   
  29.      System.out.println("strFoo.getX="+strFoo.getX());  
  30.      System.out.println("douFoo.getX="+douFoo.getX());  
  31.      System.out.println("objFoo.getX="+objFoo.getX());  
  32.   }  
  33. }  


3)泛型方法

是否拥有泛型方法,与其所在的类是否泛型没有关系。要定义泛型方法,只需将泛型参数列表置于返回值前。

  1. public class ExampleA {  
  2.   
  3.   public <T> void f(T x) {  
  4.      System.out.println(x.getClass().getName());  
  5.   }  
  6.   
  7.   public static void main(String[] args) {  
  8.   
  9.      ExampleA ea = new ExampleA();  
  10.   
  11.      ea.f("ea ");  
  12.      ea.f(10);  
  13.      ea.f('a');  
  14.   }  
  15. }  


 

  使用泛型方法时,不必指明参数类型,编译器会自己找出具体的类型。泛型方法除了定义不同,调用就像普通方法一样。需要注意,一个static方法,无法访问泛型类的类型参数,所以,若要static方法需要使用泛型能力,必须使其成为泛型方法。

 

4)限制泛型和通配符

class GenericsFoo<T extends Collection>,这样类中的泛型T只能是Collection接口的实现类,传入非Collection接口编译会出错。

class GenericsFoo<? extends Collection>,“?”代表未知类型(通配符),这个类型是实现Collection接口。<? extends 类型>表示这个类型是某个类型的子类型。

原创粉丝点击