Android(java)學習筆記92:泛型進階之萬用字元

來源:互聯網
上載者:User

標籤:

 1 package cn.itcast_07; 2  3 import java.util.ArrayList; 4 import java.util.Collection; 5  6 /* 7  * 泛型進階(萬用字元) 8  * ?:任意類型,如果沒有明確,那麼就是Object以及任意的Java類了 9  * ? extends E:向下限定,E及其子類10  * ? super E:向上限定,E極其父類11  */12 public class GenericDemo {13     public static void main(String[] args) {14         // 泛型如果明確的寫的時候,前後必須一致15         Collection<Object> c1 = new ArrayList<Object>();16         // Collection<Object> c2 = new ArrayList<Animal>();17         // Collection<Object> c3 = new ArrayList<Dog>();18         // Collection<Object> c4 = new ArrayList<Cat>();19 20         // ?表示任意的類型都是可以的21         Collection<?> c5 = new ArrayList<Object>();22         Collection<?> c6 = new ArrayList<Animal>();23         Collection<?> c7 = new ArrayList<Dog>();24         Collection<?> c8 = new ArrayList<Cat>();25 26         // ? extends E:向下限定,E及其子類27         // Collection<? extends Animal> c9 = new ArrayList<Object>();28         Collection<? extends Animal> c10 = new ArrayList<Animal>();29         Collection<? extends Animal> c11 = new ArrayList<Dog>();30         Collection<? extends Animal> c12 = new ArrayList<Cat>();31 32         // ? super E:向上限定,E極其父類33         Collection<? super Animal> c13 = new ArrayList<Object>();34         Collection<? super Animal> c14 = new ArrayList<Animal>();35         // Collection<? super Animal> c15 = new ArrayList<Dog>();36         // Collection<? super Animal> c16 = new ArrayList<Cat>();37     }38 }39 40 class Animal {41 }42 43 class Dog extends Animal {44 }45 46 class Cat extends Animal {47 }

 

Android(java)學習筆記92:泛型進階之萬用字元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.