Java基础:泛型

示例代码:

  1. class Gen<T extends Number>{    //泛型类T是泛型类型形参 extends Number约束类型
  2.     T ob;                //声明对象ob
  3.     Gen(T o){        //构造方法
  4.         ob=o;
  5.     }
  6.    <T extends Number>Gen(T o){ //泛型构造
  7.  
  8.    }    
  9.  
  10.     T getob() {    //返回T类型
  11.         return ob;
  12.     }
  13.     
  14.     void showType() {
  15.         System.out.println("Type of T is: "+ob.getClass().getName());
  16.     }
  17. }
  18.  
  19. public class GenDemo {
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         Gen<Integer> iOb=new Gen<Integer>(88);  //Integer类型的引用
  24.         iOb.showType();
  25.         
  26.         int v=iOb.getob();
  27.         System.out.println("value: "+v);
  28.         
  29.         Gen<String> strOb=new Gen<String>("Generics Test.");//String类型的引用
  30.         strOb.showType();
  31.         
  32.         String str=strOb.getob();
  33.         System.out.println("value: "+str);
  34.     }
  35. }
  36. ----------------------------------------------------------------------------------------------
  37. 带有两个类型形参的泛型类
  38.  
  39. class TwoGen<T,V>{
  40.     T ob1;
  41.     V ob2;
  42.     
  43.     TwoGen(T o1,V o2){
  44.         ob1=o1;
  45.         ob2=o2;
  46.     }
  47.     
  48.     void showTypes() {
  49.         System.out.println("Type of T is "+ob1.getClass().getName());
  50.         System.out.println("Type of V is "+ob2.getClass().getName());
  51.     }
  52.     
  53.     T getob1() {
  54.         return ob1;
  55.     }
  56.     
  57.     V getob2() {
  58.         return ob2;
  59.     }
  60.     
  61.     boolean isSame(TwoGen<T,V> o) {
  62.         if(ob1==o.ob1&&ob2==o.ob2) {
  63.             return true;
  64.         }else {
  65.             return false;
  66.         }
  67.     }
  68. }
  69.  
  70. public class SimpGen {
  71.  
  72.     public static void main(String[] args) {
  73.  
  74.         TwoGen<Integer,String> tgObj=new TwoGen<Integer,String>(88,"Generics");
  75.         
  76.         tgObj.showTypes();
  77.         
  78.         int v=tgObj.getob1();
  79.         System.out.println("Value: "+v);
  80.         
  81.         String str=tgObj.getob2();
  82.         System.out.println("Value: "+str);
  83.         
  84.         TwoGen<Integer,String> tt=new TwoGen<>(77,"hello world");//<>泛型类型推断
  85.         System.out.println(tt.getob1()+"   "+tt.getob2());
  86.         System.out.println(tt.isSame(new TwoGen<>(67,"hello world")));     
  87.     }
  88. }
  89. ---------------------------------------------------------------------------
  90. 通配符实参?
  91. boolean absEqual(Numeric<?> ob) {
  92.         if(Math.abs(num.doubleValue())==Math.abs(ob.num.doubleValue())) {
  93.             return true;
  94.         }else {
  95.             return false;
  96.         }                
  97.     }
  98. --------------------------------------------------------------------------
  99. 泛型接口
  100. public interface IGenQ<T> {
  101.     void put(T ch) throws QueueFullException;
  102.     T get() throws QueueEmptyException;
  103. }
  104. ---------------------------------------------------------------------------
  105. 愿类型和遗留代码:非泛型代码能够处理泛型代码,泛型代码能够处理非泛型代码
  106. public class RawDemo {
  107.     public static void main(String[] args) {
  108.  
  109.         Gen<Integer> iOb=new Gen<Integer>(88);
  110.         System.out.println(iOb.getob());
  111.         
  112.         Gen<String> strOb=new Gen<String>("Generics Test");
  113.         System.out.println(strOb.getob());
  114.         
  115.         Gen raw=new Gen(new Double(98.6));//不提供类型实参时,创建原类型
  116.         
  117.         double d=(Double)raw.getob();
  118.         System.out.println("value: "+d);
  119.         
  120.         Gen rbw=new Gen(new Integer(77));
  121.         int i=(Integer)rbw.getob();
  122.         System.out.println(i);
  123.     }
  124. }
     

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87989867