Java泛型編程


內容:

1、泛型概述

2、使用泛型的好處

3、泛型的定義和使用

4、泛型通配符

 

 

 

1、泛型概述

泛型:泛泛的類型,就是一種不確定的類型(JDK1.5的一個新特性)

基本體現: <E>就是泛型,此處的E是什么數據類型?

泛型用來靈活地將數據類型應用到不同的類、方法、接口當中。將數據類型作為參數傳遞。

泛型是數據類型的一部分,我們將類名與泛型合並一起看做數據類型。

泛型的定義:定義泛型可以在類中預支地使用未知的類型。

泛型的使用:一般在創建對象時,將未知的類型確定具體的類型。當沒有指定泛型時,默認類型為Object類型。

 

 

2、使用泛型的好處

好處如下:

  • 將運行時期的ClassCastException,轉移到了編譯時期變成了編譯失敗
  • 避免了類型強轉的麻煩
 1 public class GenericDemo {
 2 
 3     public static void demo1() {
 4         // 創建集合對象,不使用泛型
 5         // 沒有明確集合存儲的數據類型是什么, 什么都能存 Object
 6         ArrayList array = new ArrayList();
 7         array.add("abc");
 8         array.add(123);
 9 
10         Iterator it = array.iterator();
11         while (it.hasNext()) {
12             // 迭代器的方法next()返回值是什么 Object
13             // System.out.println(it.next());
14             String s = (String) it.next(); // 打印123時報錯
15             System.out.println(s);
16         }
17     }
18 
19     public static void demo2() {
20         // 使用泛型(集合中的泛型)
21         Collection<String> list = new ArrayList<String>();
22         list.add("abc");
23         list.add("itcast");
24         // list.add(5);//當集合明確類型后,存放類型不一致就會編譯報錯
25         
26         // 集合已經明確具體存放的元素類型,那么在使用迭代器的時候,迭代器也同樣會知道具體遍歷元素類型
27         Iterator<String> it = list.iterator();
28         while (it.hasNext()) {
29             String str = it.next();
30             // 當使用Iterator<String>控制元素類型后,就不需要強轉了。
31             // 獲取到的元素直接就是String類型
32             System.out.println(str.length());
33         }
34 
35     }
36 
37     public static void main(String[] args) {
38         demo1();
39         demo2();
40     }
41 }

 

 

3、泛型的定義和使用

(1)泛型的定義

泛型,用來靈活地將數據類型應用到不同的類、方法、接口當中。將數據類型作為參數進行傳遞

泛型中E的含義:

我們以ArrayList<E>為例:

類名: ArrayList<E>

方法:

  • boolean add(E e);
  • E get(int index)

畫圖說明:

 

(2)泛型類(含有泛型的類)

定義格式:修飾符 class 類名<代表泛型的變量> {  }

使用格式:創建對象時,確定泛型的類型

1 // 泛型類定義如下:
2 class ArrayList<E>{ 
3 public boolean add(E e){ }
4     public Eget(int index){  }
5 }
6 
7 // 泛型類使用
8 ArrayList<String> list = new ArrayList<String>();  // 此時上面的E就是String類型
9 ArrayList<Integer> list = new ArrayList<Integer>();  // 此時上面的E就是Integer類型

 

(3)自定義泛型類

 1 public class GenericClass<E>{//自定義的類中,可以寫<>泛型
 2    //E 表示未知的數據類型 調用者創建對象的時候,才能明確數據類型
 3     private E e;
 4     
 5     public void setE(E e){
 6         this.e = e;
 7     }
 8     
 9     public E getE(){
10         return e;
11     }
12 }
13 // 使用:
14 public class GenericClassTest {
15     public static void main(String[] args) {
16         // 對自定義的泛型類,進行測試
17         GenericClass<Integer> g = new GenericClass<Integer>();    
18         // E傳遞什么類型就是什么類型
19         g.setE(100);    
20         Integer i = g.getE();    
21         System.out.println(i);
22     }
23 }

 

(4)泛型方法

定義格式:修飾符 <代表泛型的變量> 返回值類型 方法名(參數){  }

使用格式:調用方法時,確定泛型的類型

上面實例中的setE方法和getE方法就是泛型方法

 

(5)泛型接口

定義格式:修飾符 interface接口名<代表泛型的變量> {  }

使用格式:

  • 1、實現接口時,確定泛型的類型
  • 2、實現接口,不指定泛型的類型,直到創建對象時,確定泛型的類型
 1 // 泛型接口
 2 public interface Inter <E>{
 3     public abstract void show(E e);
 4 }
 5 
 6 // 泛型接口使用1
 7 public class InterImpl implements Inter<Integer>{
 8     public void show(Integer i){
 9         System.out.println(i);
10     }
11 }
12 
13 // 泛型接口使用2
14 public class InterImpl<E>implements Inter<E>{
15     public void show(E e){
16         System.out.println(e);
17     }
18 }

 

 

4、泛型通配符

當使用泛型類或者接口時,傳遞的數據中,泛型類型不確定,可以通過通配符<?>表示。但是一旦使用泛型的通配符后,

只能使用Object類中的共性方法,集合中元素自身方法無法使用。

定義:(查看ArrayList的構造方法或者addAll方法)無法在類中使用

使用:調用方法時可以給予任意類型。參照Arraylist的構造方法或者addAll方法

  • ? extends E代表只要是E類型的子類即可
  • ? super E代表只要是E類型的父類即可

eg:

  • <? extends Animal>:表示一種泛型,這種泛型必須是Animal或Animal的子類
  • <? super Animal>:表示一種泛型,這種泛型必須是Animal或Animal的父類


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM