| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | classGen<T> {    privateT ob; // 定义泛型成员变量    publicGen(T ob) {        this.ob = ob;    }    publicT getOb() {        returnob;    }    publicvoidsetOb(T ob) {        this.ob = ob;    }    publicvoidshowType() {        System.out.println("T的实际类型是: "+ ob.getClass().getName());    }}publicclassGenDemo {    publicstaticvoidmain(String[] args) {        // 定义泛型类Gen的一个Integer版本        Gen<Integer> intOb = newGen<Integer>(88);        intOb.showType();        inti = intOb.getOb();        System.out.println("value= "+ i);        System.out.println("----------------------------------");        // 定义泛型类Gen的一个String版本        Gen<String> strOb = newGen<String>("Hello Gen!");        strOb.showType();        String s = strOb.getOb();        System.out.println("value= "+ s);    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | classGen2 {    privateObject ob; // 定义一个通用类型成员    publicGen2(Object ob) {        this.ob = ob;    }    publicObject getOb() {        returnob;    }    publicvoidsetOb(Object ob) {        this.ob = ob;    }    publicvoidshowTyep() {        System.out.println("T的实际类型是: "+ ob.getClass().getName());    }}publicclassGenDemo2 {    publicstaticvoidmain(String[] args) {        // 定义类Gen2的一个Integer版本        Gen2 intOb = newGen2(newInteger(88));        intOb.showTyep();        inti = (Integer) intOb.getOb();        System.out.println("value= "+ i);        System.out.println("---------------------------------");        // 定义类Gen2的一个String版本        Gen2 strOb = newGen2("Hello Gen!");        strOb.showTyep();        String s = (String) strOb.getOb();        System.out.println("value= "+ s);    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | publicclassStringFoo {    privateString x;    publicStringFoo(String x) {        this.x = x;    }    publicString getX() {        returnx;    }    publicvoidsetX(String x) {        this.x = x;    }}publicclassDoubleFoo {    privateDouble x;    publicDoubleFoo(Double x) {        this.x = x;    }    publicDouble getX() {        returnx;    }    publicvoidsetX(Double x) {        this.x = x;    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | publicclassObjectFoo {    privateObject x;    publicObjectFoo(Object x) {        this.x = x;    }    publicObject getX() {        returnx;    }    publicvoidsetX(Object x) {        this.x = x;    }} | 
| 1 2 3 4 5 6 7 8 9 10 | publicclassObjectFooDemo {    publicstaticvoidmain(String args[]) {        ObjectFoo strFoo = newObjectFoo(newStringFoo("Hello Generics!"));        ObjectFoo douFoo = newObjectFoo(newDoubleFoo(newDouble("33")));        ObjectFoo objFoo = newObjectFoo(newObject());        System.out.println("strFoo.getX="+ (StringFoo) strFoo.getX());        System.out.println("douFoo.getX="+ (DoubleFoo) douFoo.getX());        System.out.println("objFoo.getX="+ objFoo.getX());    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | classGenericsFoo<T> {    privateT x;    publicGenericsFoo(T x) {        this.x = x;    }    publicT getX() {        returnx;    }    publicvoidsetX(T x) {        this.x = x;    }}publicclassGenericsFooDemo {    publicstaticvoidmain(String args[]) {        GenericsFoo<String> strFoo = newGenericsFoo<String>("Hello Generics!");        GenericsFoo<Double> douFoo = newGenericsFoo<Double>(newDouble("33"));        GenericsFoo<Object> objFoo = newGenericsFoo<Object>(newObject());        System.out.println("strFoo.getX="+ strFoo.getX());        System.out.println("douFoo.getX="+ douFoo.getX());        System.out.println("objFoo.getX="+ objFoo.getX());    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | publicclassCollectionGenFoo<T extendsCollection> {    privateT x;    publicCollectionGenFoo(T x) {        this.x = x;    }    publicT getX() {        returnx;    }    publicvoidsetX(T x) {        this.x = x;    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 | publicclassCollectionGenFooDemo {    publicstaticvoidmain(String args[]) {        CollectionGenFoo<ArrayList> listFoo = null;        listFoo = newCollectionGenFoo<ArrayList>(newArrayList());        // 出错了,不让这么干。        // 需要将CollectionGenFoo<Collection>改为CollectionGenFoo<ArrayList>        // CollectionGenFoo<Collection> listFoo1 = null;        // listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());        System.out.println("实例化成功!");    }} | 
| 1 2 3 | publicclassDemo<T extendsComparable & Serializable> {    // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了} | 
| 1 2 3 4 5 6 7 8 9 10 11 | publicclassCollectionGenFooDemo {    publicstaticvoidmain(String args[]) {        CollectionGenFoo<ArrayList> listFoo = null;        listFoo = newCollectionGenFoo<ArrayList>(newArrayList());        // 出错了,不让这么干。        // CollectionGenFoo<Collection> listFoo1 = null;        // listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());        System.out.println("实例化成功!");    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | publicclassExampleA {    public<T> voidf(T x) {        System.out.println(x.getClass().getName());    }    publicstaticvoidmain(String[] args) {        ExampleA ea = newExampleA();        ea.f(" ");        ea.f(10);        ea.f(‘a‘);        ea.f(ea);    }} | 
原文:http://blog.csdn.net/zhangxichao100/article/details/51105465