在javaSE8中有很多值得我们兴奋的地方,在新的版本中新的或者更新的特征允许开发者以更有效的、更简洁的方式写代码。为了完全了解一些新特征的实现,比如lambdas,理解java的核心概念就变得十分重要了,在javaSE8中扮演这个核心角色之一的就是泛型。
这篇文章一开始对泛型进行了一个简单的介绍,并介绍了一些基本的概念。在介绍完基本概念之后我们将深入泛型的一些具体应用场景,最后我们看一下为什么泛型对于一些构造是非常重要的组成部分。
这篇文章用到的所有代码可以在这里下载。
考虑下边这个场景:你希望开发一个容器用来传递你应用程序中的对象。但是对象的类型并不是每次都相同的,因此,你需要开发一个能够处理不同类型对象的容器。考虑这个场景,达到这个目标最显然的方式是容器可以存储和恢复对象的类型,然后强制转换这个对象,下边这段代码就有这样的实现:
public class ObjectContainer { private Object obj; /** * @return the obj */ public Object getObj() { return obj; } /** * @param obj the obj to set */ public void setObj(Object obj) { this.obj = obj; }
ObjectContainer myObj = new ObjectContainer(); // store a string myObj.setObj("Test"); System.out.println("Value of myObj:" + myObj.getObj()); // store an int (which is autoboxed to an Integer object) myObj.setObj(3); System.out.println("Value of myObj:" + myObj.getObj()); List objectList = new ArrayList(); objectList.add(myObj); // We have to cast and must cast the correct type to avoid ClassCastException! String myStr = (String) ((ObjectContainer)objectList.get(0)).getObj(); System.out.println("myStr: " + myStr);
下边这段代码展示了怎么创建一个容器,但是这次使用了泛型类型作为参数而不是Object类型
public class GenericContainer<T> { private T obj; public GenericContainer(){ } // Pass type in as parameter to constructor public GenericContainer(T t){ obj = t; } /** * @return the obj */ public T getObj() { return obj; } /** * @param obj the obj to set */ public void setObj(T t) { obj = t; } }
为了使用这个泛型容器,你必须指定容器的类型。因此下边这段代码将会用类型Integer来指定泛型容器的类型。
GenericContainer<Integer> myInt = new GenericContainer<Integer>();
myInt.setObj(3); // OK myInt.setObj("Int"); // Won't Compile
List myObjList = new ArrayList(); // Store instances of ObjectContainer for(int x=0; x <=10; x++){ ObjectContainer myObj = new ObjectContainer(); myObj.setObj("Test" + x); myObjList.add(myObj); } // Get the objects we need to cast for(int x=0; x <= myObjList.size()-1; x++){ ObjectContainer obj = (ObjectContainer) myObjList.get(x); System.out.println("Object Value: " + obj.getObj()); } List<GenericContainer> genericList = new ArrayList<GenericContainer>(); // Store instances of GenericContainer for(int x=0; x <=10; x++){ GenericContainer<String> myGeneric = new GenericContainer<String>(); myGeneric.setObj(" Generic Test" + x); genericList.add(myGeneric); } // Get the objects; no need to cast to String for(GenericContainer<String> obj:genericList){ String objectString = obj.getObj(); // Do something with the string...here we will print it System.out.println(objectString); }
public class GenericContainer<T> { ...
E: Element K: Key N: Number T: Type V: Value S, U, V, and so on: Second, third, and fourth types in a multiparameter situation
GenericContainer<String> stringContainer = new GenericContainer<String>();
GenericContainer gc1 = new GenericContainer(3); GenericContainer gc2 = new GenericContainer("Hello");
GenericContainer rawContainer = new GenericContainer();
public class MultiGenericContainer<T, S> { private T firstPosition; private S secondPosition; public MultiGenericContainer(T firstPosition, S secondPosition){ this.firstPosition = firstPosition; this.secondPosition = secondPosition; } public T getFirstPosition(){ return firstPosition; } public void setFirstPosition(T firstPosition){ this.firstPosition = firstPosition; } public S getSecondPosition(){ return secondPosition; } public void setSecondPosition(S secondPosition){ this.secondPosition = secondPosition; } }
MultiGenericContainer<String, String> mondayWeather = new MultiGenericContainer<String, String>("Monday", "Sunny"); MultiGenericContainer<Integer, Double> dayOfWeekDegrees = new MultiGenericContainer<Integer, Double>(1, 78.0); String mondayForecast = mondayWeather.getFirstPosition(); // The Double type is unboxed--to double, in this case. More on this in next section! double sundayDegrees = dayOfWeekDegrees.getSecondPosition();
说明:本文有选择性的翻译了文章中的一部分,关于全文的内容,读者可以访问下边的链接查看原文。
原文地址:http://www.oracle.com/technetwork/articles/java/juneau-generics-2255374.html
其他资料:https://dzone.com/articles/5-things-you-should-know-about-java-generics
http://www.journaldev.com/1663/java-generics-tutorial-example-class-interface-methods-wildcards-and-much-more
http://blog.csdn.net/wangjian223344/article/details/16846165
http://www.imooc.com/article/1935