Java泛型全面解析__泛型

來源:互聯網
上載者:User

Java中的泛型可以說讓新手一頭霧水,而且多個泛型同時使用會讓人一臉懵逼。現在就來看看各個情況吧。
1.最簡單的泛型舉例

public class TTest<T> {    public void test(){        List<T> list = new ArrayList<>();    }    public static void main(String args[]){        TTest<String> t = new TTest<String>();        t.test();    }}

這個就是最簡單的泛型,可以看到在TTest類定義的地方寫了一個<T>,這就是泛型的定義,類裡面方法(靜態方法除外),都可以使用這個類,具體是什麼意思呢。

可以在main()方法裡面看到,TTest<String> t ,這裡就是把<String>傳入了進去,test()方法裡面的List<T> t = new ArrayList<>();其實就變成了List<String> t = new ArrayList<>();泛型的主要意義,也就是為了限定或者給定某個類型。當然泛型的這個<T>名字可以隨便取比如<U>,<X>等等。 2.多個泛型的使用

public class TTest<T, U> {    public void test(){        List<T> tList = new ArrayList<>();        List<U> uList = new ArrayList<>();    }    public static void main(String args[]){        TTest<String, Integer> t = new TTest<>();        t.test();    }}
這裡使用了兩個泛型<T, U>,在傳入的時候,寫入TTest<String,Integer> t 然後在類裡面就可以去輕鬆愉快的使用了,兩個泛型也是比較常見的,比如HashMap<K, V>。
3.泛型的繼承
public class TTest<T extends TTest.Base> {    public static class Base{    }    public static class Child extends Base{    }    public void test(){        List<T> tList = new ArrayList<>();    }    public static void main(String args[]){        TTest<String> st = new TTest<>();//編譯錯誤        TTest<Base> bt = new TTest<>();//編譯正確        TTest<Child> ct = new TTest<>();//編譯正確    }}

這裡可以看到<T extends TTest.Base>也就是說,我傳入的泛型是要繼承自Base類的,所以在new TTest的時候可以看到,如果寫了TTest<String>就會編譯錯誤,因為String不是繼承自Base的。寫成TTest<Base>或者TTest<Child>都是正確的,因為Child是繼承自Base的。
4.在靜態方法中泛型的使用
public class TTest<T> {    public static <M> List<M> get(){        Class<M> m;//編譯正確        Class<T> t;//編譯錯誤        return null;    }    public void test(){        TTest<Integer>.<String>get();//編譯錯誤,因為調用靜態方法不需要寫類的泛型        TTest.<String>get();//編譯正確    }}

因為靜態方法其實是屬於類的,不屬於對象,所以普通定義的泛型,在靜態方法裡面是無法使用的,會編譯錯誤,比如在上面代碼中get()方法裡面的Class<T>就會編譯錯誤。

在如果要在靜態方法中使用的泛型的話,需要重新定義,在static關鍵字和傳回型別中間添加泛型<M>,然後在傳回型別或者方法體中,就可以去使用這個泛型了。在使用的時候,就用TTest.<String>get();去調用,public static <M> List<M>get()方法就會變成public static List<String>get(),方法體裡面的Class<M>也就變成了Class<String>,其實就是把<M>替換成了<String>。


小節:

前面講到了好幾種泛型的使用情境,使用泛型的好處就是,你知道了傳入的類型,就可以在方法定義時直接轉為想要的類型,而不需要方法的調用者再去強轉,android裡面的,findViewById()方法就是一個很好的例子,新版本就是使用了泛型,不需要調用者再強轉控制項的類型了。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.