| 1234567891011121314151617181920212223242526272829303132333435 | 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 GenDemo { public static void main(String[] args) { // 定义泛型类Gen的一个Integer版本 Gen<Integer> intOb = new Gen<Integer>(88); intOb.showType(); int i = intOb.getOb(); System.out.println("value= " + i); System.out.println("----------------------------------"); // 定义泛型类Gen的一个String版本 Gen<String> strOb = new Gen<String>("Hello Gen!"); strOb.showType(); String s = strOb.getOb(); System.out.println("value= " + s); }} |
| 1234567891011121314151617181920212223242526272829303132333435 | class Gen2 { private Object ob; // 定义一个通用类型成员 public Gen2(Object ob) { this.ob = ob; } public Object getOb() { return ob; } public void setOb(Object ob) { this.ob = ob; } public void showTyep() { System.out.println("T的实际类型是: " + ob.getClass().getName()); }} public class GenDemo2 { public static void main(String[] args) { // 定义类Gen2的一个Integer版本 Gen2 intOb = new Gen2(new Integer(88)); intOb.showTyep(); int i = (Integer) intOb.getOb(); System.out.println("value= " + i); System.out.println("---------------------------------"); // 定义类Gen2的一个String版本 Gen2 strOb = new Gen2("Hello Gen!"); strOb.showTyep(); String s = (String) strOb.getOb(); System.out.println("value= " + s); }} |
| 12345678910111213141516171819202122232425262728293031 | public class StringFoo { private String x; public StringFoo(String x) { this.x = x; } public String getX() { return x; } public void setX(String x) { this.x = x; }} public class DoubleFoo { private Double x; public DoubleFoo(Double x) { this.x = x; } public Double getX() { return x; } public void setX(Double x) { this.x = x; }} |
| 123456789101112131415 | public class ObjectFoo { private Object x; public ObjectFoo(Object x) { this.x = x; } public Object getX() { return x; } public void setX(Object x) { this.x = x; }} |
| 12345678910 | public class ObjectFooDemo { public static void main(String args[]) { ObjectFoo strFoo = new ObjectFoo(new String("Hello Generics!")); ObjectFoo douFoo = new ObjectFoo(new Double(new Double("33"))); ObjectFoo objFoo = new ObjectFoo(new Object()); System.out.println("strFoo.getX=" + (String) strFoo.getX()); System.out.println("douFoo.getX=" + (Double) douFoo.getX()); System.out.println("objFoo.getX=" + objFoo.getX()); }} |
| 1234567891011121314151617181920212223242526 | 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 GenericsFooDemo { 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()); }} |
| 123456789101112131415 | public 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; }} |
| 123456789101112 | public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null; listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); // 出错了,不让这么干。 // 原来作者写的这个地方有误,需要将listFoo改为listFoo1 // 需要将CollectionGenFoo<Collection>改为CollectionGenFoo<ArrayList> // CollectionGenFoo<Collection> listFoo1 = null; // listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); }} |
| 123 | public class Demo<T extends Comparable & Serializable> { // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了} |
| 123456789101112131415161718 | public class CollectionGenFooDemo { public static void main(String args[]) { //CollectionGenFoo<ArrayList> listFoo = null; //listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); CollectionGenFoo<?> listFoo1 = null; listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); } } |
| 12345678910111213 | public class ExampleA { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { ExampleA ea = new ExampleA(); ea.f(" "); ea.f(10); ea.f('a'); ea.f(ea); }} |
| 1234567891011121314151617181920212223242526272829303132333435 | 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 GenDemo { public static void main(String[] args) { // 定义泛型类Gen的一个Integer版本 Gen<Integer> intOb = new Gen<Integer>(88); intOb.showType(); int i = intOb.getOb(); System.out.println("value= " + i); System.out.println("----------------------------------"); // 定义泛型类Gen的一个String版本 Gen<String> strOb = new Gen<String>("Hello Gen!"); strOb.showType(); String s = strOb.getOb(); System.out.println("value= " + s); }} |
| 1234567891011121314151617181920212223242526272829303132333435 | class Gen2 { private Object ob; // 定义一个通用类型成员 public Gen2(Object ob) { this.ob = ob; } public Object getOb() { return ob; } public void setOb(Object ob) { this.ob = ob; } public void showTyep() { System.out.println("T的实际类型是: " + ob.getClass().getName()); }} public class GenDemo2 { public static void main(String[] args) { // 定义类Gen2的一个Integer版本 Gen2 intOb = new Gen2(new Integer(88)); intOb.showTyep(); int i = (Integer) intOb.getOb(); System.out.println("value= " + i); System.out.println("---------------------------------"); // 定义类Gen2的一个String版本 Gen2 strOb = new Gen2("Hello Gen!"); strOb.showTyep(); String s = (String) strOb.getOb(); System.out.println("value= " + s); }} |
| 12345678910111213141516171819202122232425262728293031 | public class StringFoo { private String x; public StringFoo(String x) { this.x = x; } public String getX() { return x; } public void setX(String x) { this.x = x; }} public class DoubleFoo { private Double x; public DoubleFoo(Double x) { this.x = x; } public Double getX() { return x; } public void setX(Double x) { this.x = x; }} |
| 123456789101112131415 | public class ObjectFoo { private Object x; public ObjectFoo(Object x) { this.x = x; } public Object getX() { return x; } public void setX(Object x) { this.x = x; }} |
| 12345678910 | public class ObjectFooDemo { public static void main(String args[]) { ObjectFoo strFoo = new ObjectFoo(new String("Hello Generics!")); ObjectFoo douFoo = new ObjectFoo(new Double(new Double("33"))); ObjectFoo objFoo = new ObjectFoo(new Object()); System.out.println("strFoo.getX=" + (String) strFoo.getX()); System.out.println("douFoo.getX=" + (Double) douFoo.getX()); System.out.println("objFoo.getX=" + objFoo.getX()); }} |
| 1234567891011121314151617181920212223242526 | 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 GenericsFooDemo { 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()); }} |
| 123456789101112131415 | public 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; }} |
| 123456789101112 | public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null; listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); // 出错了,不让这么干。 // 原来作者写的这个地方有误,需要将listFoo改为listFoo1 // 需要将CollectionGenFoo<Collection>改为CollectionGenFoo<ArrayList> // CollectionGenFoo<Collection> listFoo1 = null; // listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); }} |
| 123 | public class Demo<T extends Comparable & Serializable> { // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了} |
| 123456789101112131415161718 | public class CollectionGenFooDemo { public static void main(String args[]) { //CollectionGenFoo<ArrayList> listFoo = null; //listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); CollectionGenFoo<?> listFoo1 = null; listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); } } |
| 12345678910111213 | public class ExampleA { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { ExampleA ea = new ExampleA(); ea.f(" "); ea.f(10); ea.f('a'); ea.f(ea); }} |
新闻热点
疑难解答