public class Generics<T> {
T name;
public Generics(T t){
this.name = t;
}
public void setT(T t){
this.name = t;
}
public T getT(){
return name;
}
public static void main(String[] args) {
Generics <String> g = new Generics<String>("zhang gua.");
System.out.print(g.getT());
}
}
******************************************************************
class FooDemo1<T> {
private T x;
public FooDemo1(T x) {
this.x = x;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
}
public class GenericsFooDemo{
public static void main(String args[]){
FooDemo1<String> strFoo=new FooDemo1<String>("Hello Generics!");
FooDemo1<Double> douFoo=new FooDemo1<Double>(new Double("33"));
FooDemo1<Object> objFoo=new FooDemo1<Object>(new Object());
System.out.println("strFoo.getX="+strFoo.getX());
System.out.println("douFoo.getX="+douFoo.getX());
System.out.println("objFoo.getX="+objFoo.getX());
}
}
泛型的写法种种
最新推荐文章于 2024-11-25 12:05:36 发布