JAVA 泛型方法 和 静态方法泛型

  1. /* 
  2.  
  3. //  泛型方法和静态方法泛型 
  4.  
  5.     泛型类定义的泛型 在整个类中有效 若是被方法使用 
  6.     那么泛型类的对象明确要操做的具体类型后,全部要操做的类型就已经固定 
  7.  
  8.     为了让不一样方法能够操做不一样类型  并且类型还不肯定 
  9.     那么能够将泛型定义在方法上  
  10.  
  11.      
  12. 静态方法泛型: 
  13. 静态方法不能够访问类上定义的泛型 
  14. 若是静态方法操做的应用数据类型不肯定,能够将泛型定义在方法上 
  15.  
  16.  
  17. */  
  18.   
  19. class Demo<T>  
  20. {  
  21.     public void method(T t)  
  22.     {  
  23.         System.out.println("method :"+ t);  
  24.     }  
  25.   
  26.     public static <W> void staticMethod(W w)  
  27.     {  
  28.         System.out.println("staticMethod :"+ w);  
  29.     }  
  30.   
  31.     public <K> void show( K  k)  
  32.     {  
  33.         System.out.println("Show :"+ k);  
  34.     }  
  35.   
  36.     public <Q> void print(Q q)  
  37.     {  
  38.             System.out.println("Print :"+ q);  
  39.     }  
  40. }  
  41.   
  42. class TestDemo  
  43. {  
  44.     public static void main(String [] arags)  
  45.     {  
  46.         Demo<Double> d = new Demo<Double>();  
  47.   
  48.         d.method(3.4);  
  49.           
  50.         d.staticMethod(true);  
  51.   
  52.         d.show("haha");  
  53.   
  54.         d.print(new Integer(4));  
  55.     }  
  56. }  
相关文章
相关标签/搜索