使用泛型

public class 泛型<T> {  
    private T ob; // 定义泛型成员变量  
  
    public 泛型(T ob) {  //构造方法
        this.ob = ob;  
    }  
  
    public T getOb() {  //获取泛型类型
        return ob;  
    }  
  
    public void showTyep() {  
        System.out.println("T的实际类型是: " + ob.getClass().getName());  
    }   
    
    public static void main(String[] args) {  
    	
    	泛型<Integer> intOb = new 泛型<Integer>(100);  //Integer泛型
        intOb.showTyep();  
        System.out.println("value= " + intOb.getOb());  
        System.out.println("----------------------------------");  
  
        泛型<String> strOb = new 泛型<String>("CSDN_SEU_Calvin");  //String泛型
        strOb.showTyep();  
        System.out.println("value= " + strOb.getOb());  
    } 
}

猜你喜欢

转载自blog.csdn.net/cwh0908/article/details/79348093