標籤:
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:泛型進階之萬用字元