JAVA泛型的基本使用
阿新 • • 發佈:2017-07-04
end ++ rc.d param details file println super turn
Java1.5版本號推出了泛型,盡管這層語法糖給開發者帶來了代碼復用性方面的提升,可是這只是是編譯器所做的一層語法糖,在真正生成的字節碼中,這類信息卻被擦除了。
筆者發現非常多幾年開發經驗的程序猿,依舊不善於使用Java泛型,本文將從Java泛型的基本使用入手,在今後的多篇博文裏。對泛型的使用做個總結。本文不會深入Java泛型的實現原理。僅僅會介紹Java泛型的使用。
實驗準備
首先須要創建一個類繼承體系。以商品為例,每種商品都有主要的名稱屬性。在大數據應用中,數據表和服務都能夠作為商品,表有行數屬性,服務有方法屬性。實現如代碼清單1所看到的。
代碼清單1
class Auction { private String name; public Auction(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return name; } } class Table extends Auction { private Integer rowNum; public Table(String name, Integer rowNum) { super(name); this.rowNum = rowNum; } public Integer getRowNum() { return rowNum; } public void setRowNum(Integer rowNum) { this.rowNum = rowNum; } public String toString() { return super.toString() + ", rowNum :" + rowNum; } } class Service extends Auction { private String method; public Service(String name, String method) { super(name); this.method = method; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String toString() { return super.toString() + ", method :" + method; } }
實驗:泛型最基本使用
如今為了演示泛型的使用,還是以商品為例。數據表本身僅僅是Hbase中的一張表,而服務是執行於Hadoop Yarn之上的一個數據服務,比方說是Strom、Spark或者Oozie。不管是數據表還是服務,它本身並非商品,要成為商品。必須有個裝飾器將它包裝為商品,如今我們用泛型實現一個簡單的裝飾器。見代碼清單2所看到的。
代碼清單2
class Decorator<T> { /** * * 描 述:Exp1使用<br/> * 作 者:jiaan.gja<br/> * 歷 史: (版本號) 作者 時間 凝視 <br/> * @param itemList */ public void doDecorate(List<T> itemList) { for(int i = 0; i < itemList.size(); i++) { System.out.println(itemList.get(i)); } } /** * * 描 述:Exp1使用<br/> * 作 者:jiaan.gja<br/> * 歷 史: (版本號) 作者 時間 凝視 <br/> * @param itemList * @param t */ public void addDecorate(List<T> itemList, T t) { itemList.add(t); } }
如今我們能夠使用Decorator給List加入或者打印不論什麽類型了,如代碼清單3所看到的。
代碼清單3
/** * * 類 名: Exp1<br/> * 描 述: 泛型最基本使用<br/> * 作 者: jiaan.gja<br/> * 創 建: 2015年8月13日<br/> * * 歷 史: (版本號) 作者 時間 凝視 */ class Exp1 { public static void main(String[] args) { Decorator<Auction> decoratorA = new Decorator<Auction>(); List<Auction> listA = new ArrayList<Auction>(); Auction auctionOne = new Auction("auctionOne"); Auction auctionTwo = new Auction("auctionTwo"); decoratorA.addDecorate(listA, auctionOne); decoratorA.addDecorate(listA, auctionTwo); decoratorA.doDecorate(listA); Decorator<Table> decoratorB = new Decorator<Table>(); List<Table> listB = new ArrayList<Table>(); Table tableOne = new Table("tableOne", 10); Table tableTwo = new Table("tableTwo", 20); decoratorB.addDecorate(listB, tableOne); decoratorB.addDecorate(listB, tableTwo); decoratorB.doDecorate(listB); Decorator<Service> decoratorC = new Decorator<Service>(); List<Service> listC = new ArrayList<Service>(); Service serviceOne = new Service("serviceOne", "methodOne"); Service serviceTwo = new Service("serviceTwo", "methodTwo"); decoratorC.addDecorate(listC, serviceOne); decoratorC.addDecorate(listC, serviceTwo); decoratorC.doDecorate(listC); } }
遺留問題
假設想要Auction用於Decorator,必需要聲明Decorator<Auction>;Service用於Decorator,也必須聲明Decorator<Service>。因為Table是Auction的子類型,我們自然想將Table也能用於Decorator<Auction>。就像以下這樣:
decoratorA.doDecorate(listB);
listB的泛型Table的確是decoratorA的泛型Auction的子類型,可是編譯器會報錯。說明不同意這樣的語法。
怎樣解決問題?請看下一篇《Java泛型的協變》JAVA泛型的基本使用