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 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; }