private T ob; //定義泛型成員變數
public Gen(T ob) {
this.ob = ob;
}
public T getOb() {
return ob;
}
public void setOb(T ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println(“T的實際型別是: “ + ob.getClass().getName());
}
}
public static void main(String[] args){
//定義泛型類Gen的一個Integer版本
Gen<Integer> intOb=new Gen<Integer>(88);
intOb.showTyep();
int i= intOb.getOb();
System.out.println(“value= “ + i);
System.out.println(“———————————-“);
//定義泛型類Gen的一個String版本
Gen<String> strOb=new Gen<String>(“Hello Gen!”);
strOb.showTyep();
String s=strOb.getOb();
System.out.println(“value= “ + s);
}
}
private Object ob; //定義一個通用型別成員
public Gen2(Object ob) {
this.ob = ob;
}
public Object getOb() {
return ob;
}
public void setOb(Object ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println(“T的實際型別是: “ + ob.getClass().getName());
}
}
public static void main(String[] args) {
//定義類Gen2的一個Integer版本
Gen2 intOb = new Gen2(new Integer(88));
intOb.showTyep();
int i = (Integer) intOb.getOb();
System.out.println(“value= “ + i);
System.out.println(“———————————-“);
//定義類Gen2的一個String版本
Gen2 strOb = new Gen2(“Hello Gen!”);
strOb.showTyep();
String s = (String) strOb.getOb();
System.out.println(“value= “ + s);
}
}