標籤:
一、泛型類
1 package generics; 2 /** 3 * 泛型類,格式:public class 類名<泛型型別1, ...> 4 * @author zhongfg 5 * @date 2015-06-16 6 * @param <T> 7 */ 8 class Student<T> { 9 10 private T num;11 12 public T getNum() {13 return num;14 }15 16 public void setNum(T num) {17 this.num = num;18 }19 20 }21 22 public class GenericsClass {23 public static void main(String[] args) {24 25 Student<String> s1 = new Student<String>();26 s1.setNum("001");27 String num1 = s1.getNum();28 System.out.println(num1);29 30 System.out.println("----------------");31 32 Student<Integer> s2 = new Student<Integer>();33 s2.setNum(2);34 int num2 = s2.getNum();35 System.out.println(num2);36 }37 }
二、泛型方法
1 package generics; 2 /** 3 * 泛型方法,public <泛型型別> 傳回值 方法名(泛型型別) 4 * @author zhongfg 5 * @date 2015-06-16 6 */ 7 class Actor { 8 9 public <T> void show(T t){10 11 System.out.println(t);12 }13 14 }15 16 public class GenericsMethod {17 18 public static void main(String[] args) {19 20 Actor a = new Actor();21 a.show("Angelababy");22 a.show(26);23 }24 }
三、泛型介面
1 package generics; 2 /** 3 * 泛型類,格式:public interface 介面名<泛型型別1, ...> 4 * @author zhongfg 5 * @date 2015-06-16 6 * @param <T> 7 */ 8 interface Person<T> { 9 10 public abstract void show(T t);11 }12 13 class PersonImpl<T> implements Person<T> {14 15 @Override16 public void show(T t) {17 // TODO Auto-generated method stub18 System.out.println(t);19 }20 }21 22 public class GenericsInterface {23 public static void main(String[] args) {24 25 Person<String> p1 = new PersonImpl<String>();26 p1.show("黃曉明");27 28 Person<Integer> p2 = new PersonImpl<Integer>();29 p2.show(38);30 }31 }
四、泛型進階之萬用字元
1 package generics; 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 * @author zhongfg12 * @date 2015-06-1613 */14 class Animal {15 16 }17 18 class Dog extends Animal {19 20 }21 22 class Cat extends Animal {23 24 }25 26 public class GenericsWildcard {27 public static void main(String[] args) {28 29 //明確指明泛型的時候,前後必須一致30 Collection<Object> c1 = new ArrayList<Object>();31 // Collection<Object> c2 = new ArrayList<Animal>(); 報錯,前後類型不一致32 // Collection<Object> c3 = new ArrayList<Dog>(); 同上33 // Collection<Object> c4 = new ArrayList<Cat>(); 同上34 35 //?表示任意類型都是可以的36 Collection<?> c5 = new ArrayList<Object>();37 Collection<?> c6 = new ArrayList<Animal>();38 Collection<?> c7 = new ArrayList<Dog>();39 Collection<?> c8 = new ArrayList<Cat>();40 41 //? extends E:向下限定,E及其子類42 // Collection<? extends Animal> c9 = new ArrayList<Object>(); 報錯,Object不是Animal子類43 Collection<? extends Animal> c10 = new ArrayList<Animal>();44 Collection<? extends Animal> c11 = new ArrayList<Dog>();45 Collection<? extends Animal> c12 = new ArrayList<Cat>();46 47 //? super E:向上限定,E及其父類48 Collection<? super Animal> c13 = new ArrayList<Object>(); 49 Collection<? super Animal> c14 = new ArrayList<Animal>();50 // Collection<? super Animal> c15 = new ArrayList<Dog>(); 報錯,Dog不是Animal及其以上的類型51 // Collection<? super Animal> c16 = new ArrayList<Cat>(); 同上52 }53 }
Java泛型的應用