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);
}
}
|