`

泛型的写法种种

    博客分类:
  • java
阅读更多
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());
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics