Generics 是JDK 1.5 一个最重要的特性,主要用来处理Collection。
以下代码在JDK 1.5 调试通过。
代码实例1: Demo.java
package maoxiang.examples.jdk15.generics; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map;
/** * @author 毛翔 * * 演示如何使用Generics 特性。代码来自于 Generics 教程: * http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf * * Generics类似于C++中的模板。 * 区别: * 1. * 2. */
public class Demo { public static void main(String[] args) {}
/** * 最简单的用法 */
public void Test1() {
// 以前的用法 //List myIntList = new LinkedList(); // 1 //myIntList.add(new Integer(0)); // 2 //Integer x = (Integer) myIntList.iterator().next(); // 3 需要强制转换 // 1.5 的用法 List<Integer> myIntList = new LinkedList<Integer>(); // 1’ myIntList.add(new Integer(0)); //2’ Integer x = myIntList.iterator().next(); // 3’ }
/** * 匿名字符的用法 */
public void Test2() { List<Circle> list = new ArrayList<Circle>(); //通过匿名字符打印一个集合
Wildcards(list); Wildcards1(); /* * 如果 Wildcards2 定义为Wildcards2(List <Shape> shapes) * 以下调用错误 */ Wildcards2(list); }
public void Wildcards(Collection< ? > c) { // 以前的用法 //Iterator i = c.iterator(); //for (int k = 0; k < c.size(); k++) { // log(i.next()); //}
//1.5 的用法 //Collection<?> c 表示 for (Object e : c) { log(e); } }
public void Wildcards1() { //Collection<?> c = new ArrayList<String>(); //c.add(new Object()); // compile time error
//以上为错误的用法,因为不能确定 c 的类型 ,不能使用add ,但get可以 。正确的用法如下:
ArrayList<String> c = new ArrayList<String>(); c.add("test"); List< ? > list = c; log(c.get(0)); }
public void Wildcards2(List< ? extends Shape> shapes) { //List<Shape> shapes 定义只能接受List<Shape> shapes,也不能接受 List<Circle> for (Shape s : shapes) { s.draw(); }
//以下写法错误,因为为参数申明为 extends Shpape,无法确定Rectangle 为Shape子类,属于不安全调用 //shapes.add(0, new Rectangle());
Map<String, Driver> allDrivers = new HashMap<String, Driver>(); Census.addRegistry(allDrivers); //以下写法允许,因为drivers明确定义, List<Driver> drivers = new ArrayList<Driver>(); Census.add(drivers); }
/** * Generic Methods 的用法 * */
public void Test3() { //适用于各种类型的函数 Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromArrayToCollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromArrayToCollection(sa, cs);// T inferred to be String fromArrayToCollection(sa, co);// T inferred to be Object Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); fromArrayToCollection(ia, cn);// T inferred to be Number fromArrayToCollection(fa, cn);// T inferred to be Number fromArrayToCollection(na, cn);// T inferred to be Number fromArrayToCollection(na, co);// T inferred to be Object //test.fromArrayToCollection(na, cs);// 错误用法
}
public <T> void fromArrayToCollection(T[] a, Collection<T> c) { for (T o : a) { //如果参数定义为 Collection< ? > c 以下写法错误 c.add(o); // compile time error } }
/** * generics 嵌套用法 * @param shapes */
public void drawAll(List< ? extends Shape> shapes) { List<List< ? extends Shape>> history = new ArrayList<List< ? extends Shape>>(); history.add(shapes); for (Shape s : shapes) { s.draw(); } } /** * * */ public void Test4() { List<String> l1 = new ArrayList<String>(); List<Integer> l2 = new ArrayList<Integer>(); System.out.print(l1.getClass() == l2.getClass()); //打印为 true, }
/** * 错误用法 */ public void Test5() { Collection cs = new ArrayList<String>(); //以下为错误用法 //if (cs instanceof Collection<String>) { } // illegal //以下为警告用法 //Collection<String> cstr = (Collection<String>) cs; // unchecked // warning }
public void Test6() { //错误用法 //List<String>[] lsa = new List<String>[10]; // not really allowed
List< ? >[] lsa = new List< ? >[10]; // ok, array of unbounded wildcard // type Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // correct //String s = lsa[1].get(0); // run-time error - ClassCastException //String s = lsa[1].get(0); // run time error, but we were warned String s = (String) lsa[1].get(0); // run time error, but cast is // explicit } public void Test7() { Sink<Object> s = null; Sink<String> s1 = null; Collection<String> cs = null;
String str = writeAll(cs, s1); //String str = writeAll(cs, s); // 无效调用 Object obj = writeAll1(cs, s); // 正确的调用 str = writeAll2(cs, s1); // 正确的调用 } public <T> T writeAll(Collection<T> coll, Sink<T> snk) { T last = null; for (T t : coll) { last = t; snk.flush(last); } return last; }
public <T> T writeAll1(Collection< ? extends T> coll, Sink<T> snk) { T last = null; for (T t : coll) { last = t; snk.flush(last); } return last; } public <T> T writeAll2(Collection<T> coll, Sink< ? super T> snk) { T last = null; for (T t : coll) { last = t; snk.flush(last); } return last; }
// 打印 private void log(Object ob) { System.out.print(ob); }
}
//辅助定义 abstract class Shape { public abstract void draw(); }
class Circle extends Shape { private int x, y, radius; public void draw() { } }
class Rectangle extends Shape { private int x, y, width, height; public void draw() { } }
class Person {} class Driver extends Person {} class Census { public static void addRegistry(Map<String, ? extends Person> registry) { }
public static void add(List< ? extends Person> persons) {
}
}
class Collections {
public static <T, S extends T> void copy(List<T> dest, List<S> src) {
}
}
|
代码实例2。Sink.java
package maoxiang.examples.jdk15.generics;
/**
*
* @author 毛翔
*
* 定义一个接口模板,简化了接口的定义
*
*/
interface Sink<E> {
public void flush(E t);
}
/*
* 如果是以前的定义,则要定义要各种类型的接口,显然更麻烦
* interface Sink {
*
* public void flush(String str);
* public void flush(Object obj);
* public void flush(Integer test);
* ......
* }
*/ |
 
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
|