public class Gen<T> {
private T t;
public Gen(T t){
this.t = t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public void showType(){
System.out.println("T的实际类型是:" + t.getClass().getName());
}
public static void main(String[] args) {
Gen<Integer> gen = new Gen<Integer>(1);
gen.showType();
int i = gen.getT();
System.out.println(" value = " + i);
System.out.println(" ====================== ");
//定义泛型类Gen的一个String的版本
Gen<String>strObj = new Gen<String>("Hello Gen!");
strObj.showType();
String s = strObj.getT();
System.out.println(" value = " + s);
}
}
public class Gen2 {
// 定义一个通用类型成员
private Object obj;
public Gen2(Object obj) {
this.obj = obj;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public void showType() {
System.out.println("T的实际类型是: " + obj.getClass().getName());
}
public static void main(String[] args) {
// 定义类Gen2的一个Integer版本
Gen2 intObj = new Gen2(2);
intObj.showType();
int i = (Integer) intObj.getObj();
System.out.println(" value = " + i);
System.out.println(" ====================== ");
// 定义类Gen2的一个String版本
Gen2 strOb = new Gen2("Hello Gen!");
strOb.showType();
String s = (String) strOb.getObj();
System.out.println(" value= " + s);
}
}
GenericsFoo<Double> douFoo=new GenericsFoo<Double>(new Double("33"));
GenericsFoo douFoo=new GenericsFoo(new Double("33"));
<T extends SomeClass & interface1 & interface2 & interface3>
public class Demo<T extends Comparable & Serializable> {
// T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了
}
public class GenericMethod {
public <T> void print(T x) {
System.out.println(x.getClass().getName());
}
public static void main(String[] args) {
GenericMethod method = new GenericMethod();
method.print(" ");
method.print(10);
method.print('a');
method.print(method);
}
}
public void write(Integer i, Integer[] ia); public void write(Double d, Double[] da);
public <T> void write(T t, T[] ta);
public class TestClassDefine<T, S extends T>{......}
public <T, S extends T> T testGenericMethodDefine(T t, S s){......}
List<String> list; list = new ArrayList<String>;
public class MyList<E> extends ArrayList<E> implements List<E> {...}
List<?> unknownList; List<? extends Number> unknownNumberList; List<? super Integer> unknownBaseLineIntgerList;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有