Java泛型的應用

來源:互聯網
上載者:User

標籤:

一、泛型類

 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泛型的應用

聯繫我們

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