Gen.java
GenDemo2.java
package com.zyl;
public class Gen<T> {
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 showType() {
System.out.println("T的实际类型是: " + ob.getClass().getName());
}
}
Gen2.java
package com.zyl;
public class Gen2 {
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 showType() {
System.out.println("T的实际类型是:" + ob.getClass().getName());
}
}
GenDemon.java
package com.zyl;
public class GenDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gen<Integer> intOb=new Gen<Integer>(88);
intOb.showType();
int i = intOb.getOb();
System.out.println("Value="+i);
Gen<String> strOb=new Gen<String>("HelloWorld!");
strOb.showType();
String s = strOb.getOb();
System.out.println("Value="+s);
}
}
GenDemo2.java
package com.zyl;
import org.omg.CORBA.PUBLIC_MEMBER;
public class GenDemo2 {
public static void main(String[] args) {
Gen2 intOb = new Gen2(new Integer(88));
intOb.showType();
int i = (Integer) intOb.getOb();
System.out.println("value= " + i);
System.out.println("---------------------------------");
Gen2 strOb = new Gen2("Hello Gen!");
strOb.showType();
String s = (String) strOb.getOb();
System.out.println("value= " + s);
}
}