首先請看如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class generictype { public static void main(String str[]) { Hashtable h = new Hashtable(); h.put(1, "String類型" ); int a = (String) h.get(1); System.out.println(a); } } //執(zhí)行結(jié)果 String類型 //如果我們將上述由紅色標(biāo)出的String改為int執(zhí)行后結(jié)果如下(更改后編譯沒有錯誤): Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at genetictype.generictype.main(generic1.java:10) |
以上就是強(qiáng)制類型轉(zhuǎn)換可能帶來的典型錯誤,然而這個錯誤在編譯期間無法知道,以至于在運(yùn)行期間jvm檢查后拋出類型轉(zhuǎn)換異常。
再看下述代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class generictype { public static void main(String str[]) { Hashtable<Integer, String> h = new Hashtable<Integer, String>(); h.put(1, "String類型" ); String a= h.get(1); System.out.println(a); } } //執(zhí)行結(jié)果 string類型 //需要提出的是1.上述由紅色標(biāo)出的String如果改為int,在編譯的時候會報錯 2.在h.get(1)前面不需要再進(jìn)行強(qiáng)制類型轉(zhuǎn)換。 |
綜上看來泛型的作用為:
1.就是是在編譯的時候檢查類型的安全(解決java中強(qiáng)制類型轉(zhuǎn)換可能導(dǎo)致的錯誤),交給了編譯器巨大的使命。
2.提高代碼的重用率
類型擦除:
類型擦除就是說編譯器編譯.java文件時,將類的泛型參數(shù)去掉,那么jvm加載字節(jié)碼文件的時候?qū)Ψ盒筒豢梢姡@個過程就稱為類型擦除。
與類型擦除有關(guān)的現(xiàn)象:
(1) 泛型類沒有Class的類類型。比如并不存在List<String>.class或是List<Integer>.class,而只有List.class。
(2) 靜態(tài)變量是被泛型類的所有實(shí)例所共享的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class generictype { public static void main(String str[]){ test1<String> t = new test1<String>(); test1<Date> tt = new test1<Date>(); System.out.println(t.a); System.out.println(tt.a); } } class test1<T>{ static int a = 1; } //結(jié)果 1 |
(3) 泛型的類型參數(shù)錯誤不能通過異常處理,因為異常處理是jvm實(shí)現(xiàn)的,而jvm加載的字節(jié)碼文件已經(jīng)擦除了泛型特征,這也間接的說明了泛型的意義:在編譯期間發(fā)現(xiàn)參數(shù)類型錯誤。
類型擦除的基本過程也比較簡單:
1.將類型參數(shù)用頂級父類替換,這類一般是Object,如果指定了類型參數(shù)的上界的話,則使用這個上界。
2.去掉出現(xiàn)的類型聲明,即去掉<>的內(nèi)容。
例如:T get()方法聲明就變成了Object get();List<String>就變成了List。接下來就可能需要生成一些橋接方法(bridge method)。這是由于擦除了類型之后的類可能缺少某些必須的方法。比如考慮下面的代碼:
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
|
public class generictype { public static void main(String str[]) { test3 t = new test3(); t.getT( "11111" ); } } interface test2<T>{ public T getT(T t); } class test3 implements test2<String>{ public String getT(String t){ return t; } } //類型擦除后的代碼 public class generictype { public static void main(String str[]) { test3 t = new test3(); t.getT( "11111" ); } interface test2 { public Object getT(Object t); } class test3 implements test2 { public String getT(String T){ return T } public Object getT(Object t) { return this .getT((String) t); } //如果沒有這段代碼,在類型擦除后test3沒有重寫接口test2的抽象方法,明顯錯誤,因此編譯器的巨大作用就是在這里幫忙生成了該方法,同時編譯器也依靠該功能完成檢錯任務(wù)。 } |
泛型的分類:泛型類,泛型接口,泛型方法,泛型異常
泛型類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class generictype { public static void main(String str[]) { test<Integer, String> t = new test<Integer, String>(); t.put( 1 , "str1" ); t.put( 2 , "str2" ); System.out.println(t.get( 1 )); System.out.println(t.get( 2 )); } } class test<T, V> { public Hashtable<T, V> h = new Hashtable<T, V>(); public void put(T t, V v) { h.put(t, v); } public V get(T t) { return h.get(t); } } //執(zhí)行結(jié)果 str1 str2 |
多態(tài)方法(泛型方法):在函數(shù)名前定義泛型參數(shù),可以在傳入?yún)?shù)列表,返回值類型,方法體里面引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class generictype { public <T> String getString(T obj){ return obj.toString(); } public static void main(String str[]) { generictype g = new generictype (); //不需要類的泛型 System.out.println(g.getString( 1 )); System.out.println(g.getString( 'a' )); System.out.println(g.getString( "a" )); } } //執(zhí)行結(jié)果 a a |
泛型異常(兼具泛型接口)
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
|
public class generictype { public static void main(String str[]) { TestException t = new TestException(); try { t.excute( 2 ); } catch (IOException e) { e.printStackTrace(); } } } //extends說明該泛型參數(shù)繼承于Exception interface TestExceptionInterface<T extends Exception> { public void excute( int i) throws T; } class TestException implements TestExceptionInterface<IOException>{ @Override public void excute( int i) throws IOException { if (i< 10 ){ throw new IOException(); } } } //意義:1.針對不同的可能出現(xiàn)的異常類型,定義自己的實(shí)現(xiàn)類。 2 .定義多個實(shí)現(xiàn)類的時候,不用一個一個手動 throws 異常,提高了代碼重用率 |
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/kundeg/p/6591621.html