一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java泛型詳解(二)

java泛型詳解(二)

2020-08-31 14:17unbelievableme Java教程

本文主要介紹了java泛型的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧

首先請看如下代碼:

?
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄色网页 | 日本一片免费观看高清完整 | bl双性小说 | 日本乱中文字幕系列在线观看 | 日本天堂视频在线观看 | 亚洲欧美自偷自拍另类小说 | 亚洲人成网站在线观看青青 | 欧美成人精品第一区二区三区 | 狠狠色狠狠色综合曰曰 | 97超pen个人视频公开视频视 | www射com| 成人高辣h视频一区二区在线观看 | 亚洲国产欧美在线人成aaaa20 | 日本动漫黄网站在线观看 | 五月激情丁香婷婷综合第九 | 亚久久伊人精品青青草原2020 | 韩国美女主播在线 | 91免费在线 | 黑帮少爷爱上我第8集在线观看 | 青春草在线观看视频 | 性欧美sexovideotv | 青柠影视在线播放观看高清 | 99久久免费视频 | 亚洲视频免费在线观看 | 欧美 变态 另类 人妖班 | 亚洲国产精品婷婷久久久久 | 三级全黄的视频 | 亚洲七七久久综合桃花 | 国产精品va在线观看无 | 五月激激激综合网色播免费 | 国产嘿咻 | 国亚洲欧美日韩精品 | 我要色色网 | 毛茸茸的大逼 | 亚洲剧情在线 | 丝袜足液精子免费视频 | 色综合久久久 | 成人在线视频在线观看 | 成人免费视频一区 | 国产男技师 | 成人高清视频在线观看 |