Java泛型

感觉和c++的模板很类似,这里记录一下基本的认识

 1 public class Test<T> {
 2     private T obj;
 3     public Test(T obj){
 4         this.obj = obj;
 5     }
 6     public T getObj(){
 7         return this.obj;
 8     }
 9     public void setObj(T obj){
10         this.obj = obj;
11     }
12     public void showType(){
13         System.out.println("the type is:" + obj.getClass().getName());
14     }
15     public static void main(String[] args) {
16         Test<String> test1 = new Test<String>("string");
17         test1.showType();
18         Test<Integer> test2 = new Test<Integer>(24);
19         test2.showType();
20     }
21     
22 
23 }
原文地址:https://www.cnblogs.com/luckygxf/p/4089575.html