泛型類定義和泛型方法以及泛型限定

來源:互聯網
上載者:User

標籤:style   blog   java   使用   strong   os   

1、泛型類定義的泛型,在整個類中有效。如果被方法使用,那麼泛型類的對象明確要操作的具體類型後,所有要操作的類型就已經固定了。

2、為了讓不同方法可以操作不同類型,而且類型還不確定。那麼可以將泛型定義在方法上。

3、特殊之處:

靜態方法不可以訪問類上定義的泛型。

如果靜態方法操作的應用資料類型不確定,可以將泛型定義在方法上。

package tan;//定義泛型類(要操作的類型不確定)class Demo<T>{public void show(T t){System.out.println("show:"+t);}//方法中定義泛型public <Q> void print(Q q){System.out.println("pirnt:"+q);}//靜態方法中定義泛型public static <G> void method(G g){System.out.println("mentod:"+g);}}public class GenericDemo1 {public static void main(String[] args) {Demo<String> d=new Demo<String>();d.show("beijing");  //d.show(6); 編譯失敗,因為show方法會隨著對象的類型,對象是什麼類型的,show方法中就只能接受什麼類型d.print(888);//將泛型定義在方法上傳什麼類型都可以d.print("tan");//類名調用靜態方法Demo.method("IT Dreamer");}}


4、將泛型定義在介面上

package tan;//將泛型定義在介面上interface Inter<T>{public void show(T t);}//確定操作類型/*class InterImpl implements Inter<String>{@Overridepublic void show(String t) {System.out.println("show-----"+t);}}*///方式二:實現介面以後仍然不知操作類型時class InterImpl<T> implements Inter<T>{@Overridepublic void show(T t) {System.out.println("show-----"+t);}}public class GenericDemo2 {public static void main(String[] args) {/*InterImpl impl=new InterImpl();impl.show("Great");*/InterImpl<Integer> i = new InterImpl<Integer>();i.show(4);}}

5、? 萬用字元。也可以理解為預留位置。

package tan;import java.util.*;public class GenericDemo3 {public static void main(String[] args) {ArrayList<String> al = new ArrayList<String>();al.add("abc1");al.add("abc2");al.add("abc3");ArrayList<Integer> al1 = new ArrayList<Integer>();al1.add(4);al1.add(7);al1.add(1);printColl(al);printColl(al1);}//1、不明確類型用預留位置表示public static void printColl(ArrayList<?> al){Iterator<?> it = al.iterator();while(it.hasNext()){System.out.println(it.next().toString());}}//2、也可以用下面這種方式:如果T是一個具體類型的話,可以接收並操作具體類型public static <T>void printColl2(ArrayList<T> al){Iterator<T> it = al.iterator();while(it.hasNext()){T t=it.next();System.out.println(t);}}}

6、泛型限定

? extends E: 可以接收E類型或者E的子類型。上限。
? super E: 可以接收E類型或者E的父類型。   下限


上限樣本

package xiao;import java.util.*;public class GenericDemo3 {public static void main(String[] args) {ArrayList<Person> al = new ArrayList<Person>();al.add(new Person("tan11"));al.add(new Person("tan13"));al.add(new Person("tan21"));ArrayList<Student> al1 = new ArrayList<Student>();al1.add(new Student("stu01"));al1.add(new Student("stu02"));al1.add(new Student("stu03"));printColl(al);System.out.println();printColl2(al);printColl2(al1);}// 1、只能列印父類類型public static void printColl(ArrayList<Person> al) {Iterator<Person> it = al.iterator();while (it.hasNext()) {System.out.println(it.next().getName());}}// 2.既可以列印父類也可以列印子類類型       《 上限》? extends E: 可以接收E類型或者E的子類型public static void printColl2(ArrayList<? extends Person> al) {Iterator<? extends Person> it = al.iterator();while (it.hasNext()) {System.out.println(it.next().getName());}}}class Person {private String name;Person(String name) {this.name = name;}public String getName() {return name;}}class Student extends Person {Student(String name) {super(name);}}/*class Student implements Comparable<Person>//<? super E>{public int compareTo(Person s){this.getName()}*/

下限樣本:

package zheng;import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;public class GenericDemo5 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(new Comp());ts.add(new Student("abc1"));ts.add(new Student("abc2"));ts.add(new Student("abc3"));Iterator<Student> it = ts.iterator();while(it.hasNext()){System.out.println(it.next().getName());}}}class Person{private String name;Person(String name){this.name = name;}public String getName(){return name;}}class Student extends Person{Student(String name){super(name);}}class Comp implements Comparator<Person>//<? super E>{public int compare(Person s1,Person s2){//Person s1 = new Student("abc1");return s1.getName().compareTo(s2.getName());}}

建立通用的比較子方法

package Qiang;import java.util.*;class GenericDemo4{public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(new Comp());ts.add(new Student("abc03"));ts.add(new Student("abc02"));ts.add(new Student("abc06"));ts.add(new Student("abc01"));Iterator<Student> it = ts.iterator();while(it.hasNext()){System.out.println(it.next().getName());}TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());ts1.add(new Worker("wabc--03"));ts1.add(new Worker("wabc--02"));ts1.add(new Worker("wabc--06"));ts1.add(new Worker("wabc--01"));Iterator<Worker> it1 = ts1.iterator();while(it1.hasNext()){System.out.println(it1.next().getName());}}}//比較子是通用的,class Comp implements Comparator<Person>{public int compare(Person p1,Person p2){//只能使用父類中的方法,有擴充性就有局限性return p2.getName().compareTo(p1.getName());}}class Person{private String name;Person(String name){this.name = name;}public String getName(){return name;}public String toString(){return "person :"+name;}}class Student extends Person{Student(String name){super(name);}}class Worker extends Person{Worker(String name){super(name);}}



聯繫我們

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