一介紹:
在JavaSE1.5之前,沒有泛型的情況的下,通過對(duì)類型Object的引用來(lái)實(shí)現(xiàn)參數(shù)的“任意化”,“任意化”帶來(lái)的缺點(diǎn)是要做顯式的強(qiáng)制類型轉(zhuǎn)換,而這種轉(zhuǎn)換是要求開發(fā)者對(duì)實(shí)際參數(shù)類型可以預(yù)知的情況下進(jìn)行的。對(duì)于強(qiáng)制類型轉(zhuǎn)換錯(cuò)誤的情況,編譯器可能不提示錯(cuò)誤,在運(yùn)行的時(shí)候才出現(xiàn)異常,這是一個(gè)安全隱患。
泛型的好處是在編譯的時(shí)候檢查類型安全,并且所有的強(qiáng)制轉(zhuǎn)換都是自動(dòng)和隱式的,提高代碼的重用率。
二、泛型參數(shù):
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
|
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的實(shí)際類型是: " + ob.getClass().getName()); } } public class GenericParameter { public static void main(String[] args){ //定義泛型類Gen的一個(gè)Integer版本 Gen<Integer> intOb= new Gen<Integer>( 100 ); intOb.showType(); int i= intOb.getOb(); System.out.println( "value= " + i); System.out.println( "----------------------------------" ); //定義泛型類Gen的一個(gè)String版本 Gen<String> strOb= new Gen<String>( "Hello Dylan!" ); strOb.showType(); String s=strOb.getOb(); System.out.println( "value= " + s); } } |
output:
T的實(shí)際類型是: java.lang.Integer
value= 100
----------------------------------
T的實(shí)際類型是: java.lang.String
value= Hello Dylan!
三、泛型類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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 GenericClass { 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()); } } |
output:
strFoo.getX=Hello Generics!
douFoo.getX=33.0
objFoo.getX=java.lang.Object@1d0fafc
四 限制泛型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.util.ArrayList; import java.util.Collection; 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; } } public class GenericRestrict { public static void main(String[] args) { CollectionGenFoo<ArrayList> listFoo = null ; listFoo = new CollectionGenFoo<ArrayList>( new ArrayList()); CollectionGenFoo<? extends Collection> listFoo1 = null ; listFoo1= new CollectionGenFoo<ArrayList>( new ArrayList()); System.out.println( "實(shí)例化成功!" ); } } |
output:
實(shí)例化成功!
五 泛型方法:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class GenericFunction { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { GenericFunction ea = new GenericFunction(); ea.f( " " ); ea.f( 10 ); ea.f( 'a' ); ea.f(ea); } } |
output:
java.lang.String
java.lang.Integer
java.lang.Character
GenericFunction
-----------------------------
dylan presents.
總結(jié)
以上就是本文關(guān)于java中generic實(shí)例詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/indexman/article/details/17485361