PDF
Bounded Generics1 JavaContentsBounded Generics .................................................................................... 1Type erase ............................................................................................. 2Q&A .................................................................................................... 4class Wrapper<T> { final T data; Wrapper(T data) { this.data = data; }}Bounded GenericsMultiple boundinterface I {}interface M {}abstract class C {}class Foo extends C implements I,M {}<T extends I & M> void bar(T arg){}<T extends C> void ooo(T arg){}<T extends C & I & M> void xxx(T arg){}Unbounded wildcards使List<A>List<B>private final List<String> strList = Arrays.asList("Hello", "World!");private final List<Integer> intList = Arrays.asList(1, 2, 3);private final List<Float> floatList = Arrays.asList(1.1f, 2.1f, 3.1f);private final List<Number> numberList = Arrays.asList(1, 1.0f, 3000L); Type erase2public void cast() { List<?> unknownList = null; unknownList = strList; unknownList = intList; unknownList = floatList; unknownList = numberList; for (int i = 0; i < unknownList.size(); i++) { // Number item = unknownList.get(i); wrong! Object item = unknownList.get(i); System.out.println(item + "(" + item.getClass() + ")"); }}/* output1(class java.lang.Integer)1.0(class java.lang.Float)3000(class java.lang.Long)*/Upper bounded wildcardspublic static double sumOfList(List<? extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s;}//...sumOfList(Arrays.asList(1, 2, 3));sumOfList(Arrays.asList(1.0f, 2.0f, 3.0f));Lower bounded wildcardspublic static void addNumbers(List<? super Number> list) { for (int i = 1; i <= 10; i++) { list.add(i); list.add(1.0f); }}addNumbers(new ArrayList<Number>());Type eraseType erase process Type erase3public class Node<T> { // public class Node { private T data; // private Object data; private Node<T> next; // private Node next; public Node(T data, Node<T> next) { // public Node(Object data, Node next) { this data = data; // this data = data; this next = next; // this next = next; } // } // public T getData() { return data; } // public Object getData() { return data; }} // }public class Node<T extends Comparable<T>> { // public class Node { private T data; // private Comparable data; private Node<T> next; // private Node next; public Node(T data, Node<T> next) { // public Node(Comparable data, Node next) { this.data = data; // this.data = data; this.next = next; // this.next = next; } // } // public T getData() { return data; } // public Comparable getData() { return data; }} // }Bridge methodpublic class MyNode extends Node<Integer> { // public class MyNode extends Node { public MyNode(Integer data) { super(data); } // public MyNode(Integer data) { super(data); } // public void setData(Integer data) { // public void setData(Integer data) { System.out.println("MyNode.setData"); // System.out.println("MyNode.setData"); super.setData(data); // super.setData(data); } // }} // }MyNode mn = new MyNode(5); // MyNode mn = new MyNode(5); Q&A4Node n = mn; // Node n = (MyNode)mn;n.setData("Hello"); // n.setData("Hello");Integer x = mn.data; // Integer x = (String)mn.data;public void setData(Object data) { setData((Integer) data);}Q&AList<?> vs List<Object>extends vs superList<?>List<Object>List<Child>List<? extends T>List<T>List<Child>List<? super T>List<T>List<Parent>List<Object>public static <T> void copy(List dest, List src)List<Parent> parents;List<T> ts;List<Child> childs;public class Collections { public static <T> void copy ( List<? super T> dest, List<? extends T> src) { // uses bounded wildcards for (int i=0; i<src.size(); i++) dest.set(i,src.get(i)); } } Q&A5

HTML view coming soon.

Download PDF for the full formatted version.