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());
}
}
public class GenericParameter {
public static void main(String[] args){
//定义泛型类Gen的一个Integer版本
Gen<Integer> intOb=new Gen<Integer>(100);
intOb.showType();
int i= intOb.getOb();
System.out.println("value= " + i);
System.out.println("----------------------------------");
//定义泛型类Gen的一个String版本
Gen<String> strOb=new Gen<String>("Hello Dylan!");
strOb.showType();
String s=strOb.getOb();
System.out.println("value= " + s);
}
}
class GenericsFoo<T> {
private T x;
public GenericsFoo(T x) {
this.x = x;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
}
public class GenericClass {
public static void main(String args[]){
GenericsFoo<String> strFoo=new GenericsFoo<String>("Hello Generics!");
GenericsFoo<double> douFoo=new GenericsFoo<double>(new double("33"));
GenericsFoo<Object> objFoo=new GenericsFoo<Object>(new Object());
System.out.println("strFoo.getX="+strFoo.getX());
System.out.println("douFoo.getX="+douFoo.getX());
System.out.println("objFoo.getX="+objFoo.getX());
}
}
import java.util.ArrayList;
import java.util.Collection;
class CollectionGenFoo<T extends Collection> {
private T x;
public CollectionGenFoo(T x) {
this.x = x;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
}
public class GenericRestrict {
public static void main(String[] args) {
CollectionGenFoo<ArrayList> listFoo = null;
listFoo = new CollectionGenFoo<ArrayList>(new ArrayList());
CollectionGenFoo<? extends Collection> listFoo1 = null;
listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());
System.out.println("实例化成功!");
}
}
public class GenericFunction {
public <T> void f(T x) {
System.out.println(x.getClass().getName());
}
public static void main(String[] args) {
GenericFunction ea = new GenericFunction();
ea.f(" ");
ea.f(10);
ea.f('a');
ea.f(ea);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有