标签:
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
|
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); } } |
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
|
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); } } |
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
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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; } } |
1
2
3
4
5
6
7
8
9
10
|
public class ObjectFooDemo { public static void main(String args[]) { ObjectFoo strFoo = new ObjectFoo( new StringFoo( "Hello Generics!" )); ObjectFoo douFoo = new ObjectFoo( new DoubleFoo( new Double( "33" ))); ObjectFoo objFoo = new ObjectFoo( new Object()); 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
|
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()); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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; } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null ; listFoo = new CollectionGenFoo<ArrayList>( new ArrayList()); // 出错了,不让这么干。 // 需要将CollectionGenFoo<Collection>改为CollectionGenFoo<ArrayList> // CollectionGenFoo<Collection> listFoo1 = null; // listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println( "实例化成功!" ); } } |
1
2
3
|
public class Demo<T extends Comparable & Serializable> { // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了 } |
1
2
3
4
5
6
7
8
9
10
11
|
public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null ; listFoo = new CollectionGenFoo<ArrayList>( new ArrayList()); // 出错了,不让这么干。 // 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
|
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); } } |
标签:
原文地址:http://www.cnblogs.com/zhangxichao/p/5350933.html