JAVA學習第四十二課 — 泛型(二)—泛型介面&&萬用字元應用,java

來源:互聯網
上載者:User

JAVA學習第四十二課 — 泛型(二)—泛型介面&&萬用字元應用,java

一、泛型介面

interface Inter<T>{public void show(T t);}class InterImple implements Inter<String>{//知道是字串類型public void show(String str){System.out.println("show "+str);}}class InterImple_2<Q> implements Inter<Q>{//不知道是什麼類型,使用的時候才知道public void show(Q q){System.out.println("InterImple_2.show()+"+q);}}public class Main {public static void main(String[] args) {InterImple in = new InterImple();in.show("sf");InterImple_2<Integer> in2 = new InterImple_2<Integer>();in2.show(2);}}


二、泛型限定 


泛型的萬用字元,通俗的說就是不管什麼類型,都用一個符號表示T,E,X,?...
其中萬用字元?和萬用字元T區別不大

import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;public class Main {public static void main(String[] args) {ArrayList<String> al = new ArrayList<String>();al.add("ads");al.add("sfdf");show(al);ArrayList<Integer> a3 = new ArrayList<Integer>();a3.add(5);a3.add(6);show(a3);HashSet<Integer> al2 =  new HashSet<Integer>();al2.add(1);al2.add(2);show(al2);}//如果單純是列印,萬用字元用?即可public static void show(Collection<?> al){Iterator<?> it = al.iterator();Iterator<?> it = al.iterator();while(it.hasNext()){System.out.println(it.next());}}//如果需要返回繼續操作//public static <T> T show(Collection<T> al) {//Iterator<T> iterator = al.iterator();//T t = iterator.next();//return t;//}}

關於迭代器

/*Iterator<T> it = al.iterator();while(it.hasNext()){System.out.println(it.next());}*///java5.0 以後用了更為優雅的for each迴圈,與iterator表示同樣的迴圈for(T x : al){System.out.println(x);}

簡潔明了
萬用字元第一種示範:

import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Main {public static void main(String[] args) {ArrayList<Worker> al = new ArrayList<Worker>();al.add(new Worker("abc",12));al.add(new Worker("dgh",11));show(al);ArrayList<Student> a3 = new ArrayList<Student>();a3.add(new Student("ASD",156));a3.add(new Student("AFDFD",16));show(a3);}//如果只想列印Man的子類//Collection<Man>不對,Collection<Man> = new ArrayList<Student>();左右類型不符//所以在泛型聲明,就可以採用繼承的方式public static void show(Collection<? extends Man> al){//Iterator<? extends Man> it = al.iterator();while(it.hasNext()){Man man = it.next();System.out.println(man);}}//等價於public static <T extends Man> void  show1(Collection<T> al){//for(T xT : al)System.out.println(xT);}}

所以泛型存的限定有兩種:
上限:?extends E,?只能就收E類型或E的子類

下限:? super E,接受E類型或E的父類


三、上限的體現:

import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Main {public static void main(String[] args) {ArrayList<Man> al1 = new ArrayList<Man>();al1.add(new Man("ABC",12));al1.add(new Man("DGH",11));ArrayList<Worker> al2 = new ArrayList<Worker>();al2.add(new Worker("abc",12));al2.add(new Worker("dgh",11));ArrayList<Student> al3 = new ArrayList<Student>();al3.add(new Student("abc",12));al3.add(new Student("dgh",11));al1.addAll(al2);//一般存元素的時候,都是上限,因為這樣取出都是按照上限類型來運算的,不會出現型別安全隱患 System.out.println(al1.size());}}

詳細看API文檔,addAll方法 : addAll(? extends E),可以儲存E及E的所有子類


四、下限的體現:

通常對集合中元素進行取出動作時,可以用下限
也就是不論存什麼類型(只要是當前父類的子類),都可以用父類型接收

import java.util.Comparator;import java.util.TreeSet;import java.util.Iterator;class ComparaName implements Comparator<Man>{//按姓名排序public int compare(Man o1, Man o2) {// TODO Auto-generated method stubint t = o1.getName().compareTo(o2.getName());return t==0?o1.getAge()-o2.getAge():t;}}public class Main {public static void main(String[] args) {TreeSet<Man> al1 = new TreeSet<Man>(new ComparaName());al1.add(new Man("ABC",12));al1.add(new Man("DGH",11));TreeSet<Worker> al2 = new TreeSet<Worker>();al2.add(new Worker("abc",12));al2.add(new Worker("dgh",11));TreeSet<Student> al3 = new TreeSet<Student>(new ComparaName());al3.add(new Student("abc",12));al3.add(new Student("dgh",11));//把學生和工人都加入al1集合al1.addAll(al3);al1.addAll(al2);//都按照Man的定序排序Iterator<Man> it = al1.iterator();while(it.hasNext())System.out.println(it.next());}}

五、萬用字元的體現

import java.util.Collection;import java.util.ArrayList;import java.util.Iterator;public class Main {public static void main(String[] args) {ArrayList<Man> al1 = new ArrayList<Man>();al1.add(new Man("ABC",12));al1.add(new Man("DGH",11));ArrayList<Man> al2 = new ArrayList<Man>();al2.add(new Man("ABC",12));al2.add(new Man("DGH",11));boolean flag1 = al1.containsAll(al2);System.out.println(flag1);ArrayList<String> al3 = new ArrayList<String>();al3.add("qwertyi");al3.add("asd");boolean flag2 = al1.containsAll(al3);System.out.println(flag2);}//方法 : containsAll(Collection <?> c) //?不論傳什麼都接收,就好比Object裡的equals方法:"asd".equals(new Worker("asd",12)),編譯和運行都是通過的public static void show(Collection<?> al){for(Iterator<?> it = al.iterator();it.hasNext();){System.out.println(it.next());}}}




Java介面泛型問題

有聲明啊 你看第二段代碼 private SeqList<Element> list; 就是定義了一個放class Element的集合。
而且第二段代碼在class SeqSparseMatrix的建構函式中還調用了裝class Element的集合。
 
學習java遇到的泛型問題,望大牛解答,感激不盡

通俗的說,泛型就是把某個類的類型作為一個參數傳遞到一個類,介面或方法中去
泛型作用是避免繁瑣的裝箱和拆箱
比如說List<A> aList=new ArrayList<A>();
往裡邊加元素就是 aList.add(a)
取的時候就直接取
for(Object o:aList){
o;//就是你存的對象
}
如果不用泛型的話
List<> aList=new ArrayList<>();
在取資料的時候必須要轉一下
for(Object o:aList){
A o1=(A)o; //o1才是你要的對象
}
 

聯繫我們

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