泛型的写法种种_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 泛型的写法种种

泛型的写法种种

 2013/7/19 0:58:03  zhangIT  程序员俱乐部  我要评论(0)
  • 摘要:publicclassGenerics<T>{Tname;publicGenerics(Tt){this.name=t;}publicvoidsetT(Tt){this.name=t;}publicTgetT(){returnname;}publicstaticvoidmain(String[]args){Generics<String>g=newGenerics<String>("zhanggua.");System.out.print(g.getT())
  • 标签:泛型
class="java" name="code">
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());
	}
}
发表评论
用户名: 匿名