一. 泛型概念的提出(为什么需要泛型)?
首先,我们看下下面这段简短的代码:public class GenericTest { /** * @param args */ public static void main(String[] args) { List list = new ArrayList(); list.add("qqyumidi"); list.add("corn"); list.add(100); for (int i = 0; i < list.size(); i++) { String name = (String)list.get(i); // 1 System.out.println("name:" + name); } } }
public class GenericTest { /** * @param args */ public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("qqyumidi"); list.add("corn"); //list.add(100); // 1 提示编译错误 for (int i = 0; i < list.size(); i++) { String name = list.get(i); // 1 System.out.println("name:" + name); } } }
public interface List<E> extends Collection<E> { int size(); boolean isEmpty(); boolean contains(Object o); Iterator<E> iterator(); Object[] toArray(); <T> T[] toArray(T[] a); boolean add(E e); boolean remove(Object o); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean addAll(int index, Collection<? extends E> c); boolean removeAll(Collection<?> c); boolean retainAll(Collection<?> c); void clear(); boolean equals(Object o); int hashCode(); E get(int index); E set(int index, E element); void add(int index, E element); E remove(int index); int indexOf(Object o); int lastIndexOf(Object o); ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); List<E> subList(int fromIndex, int toIndex); }
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } public E get(int index) { rangeCheck(index); checkForComodification(); return ArrayList.this.elementData(offset + index); } //...省略掉其他具体的定义过程 }
public class GenericTest { public static void main(String[] args) { Box<String> car = new Box<String>("wpy"); System.out.println("name:" + car.getData()); } } public class Box<T> { private T data; public Box() { } public Box(T data) { this.data = data; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
public class GenericTest2 { public static void main(String[] args) { Box<String> name = new Box<String>("corn"); Box<Integer> age = new Box<Integer>(712); System.out.println("name class:" + name.getClass()); // com.qqyumidi.Box System.out.println("age class:" + age.getClass()); // com.qqyumidi.Box System.out.println(name.getClass() == age.getClass()); // true System.out.println(name.getData()); System.out.println(age.getData()); } }
public class GenericTest3 { public static void main(String[] args) { Box<Integer> a = new Box<Integer>(712); //Box<Number> b = a; // 1 Box<Float> f = new Box<Float>(3.14f); //b.setData(f); // 2 } public static void getData(Box<Number> data) { System.out.println("data :" + data.getData()); } }
public class GenericTest4 { public static void main(String[] args) { Box<String> name = new Box<String>("corn"); Box<Integer> age = new Box<Integer>(712); Box<Number> number = new Box<Number>(314); getData(name); getData(age); getData(number); } public static void getData(Box<?> data) { System.out.println("data :" + data.getData()); } }
public class GenericTest5 { /** * 在于Java中的泛型这一概念提出的目的,导致其只是作用于代码编译阶段,在编译过程中, * 对于正确检验泛型结果后,会将泛型的相关信息擦出,也就是说,成功编译过后的class文件中是不包含任何泛型信息的。 泛型信息不会进入到运行时阶段。 * 对此总结成一句话:泛型类型在逻辑上看以看成是多个不同的类型, 实际上都是相同 的基本类型。 * * @param args */ public static void main(String[] args) { Box<String> name = new Box<String>("corn"); Box<Integer> age = new Box<Integer>(712); Box<Number> number = new Box<Number>(314); //getUpperNumberData(name); getUpperNumberData(age); getUpperNumberData(number); } public static void getData(Box<?> data) { System.out.println("data :" + data.getData()); } }
public class GenericTest6 { public static void main(String[] args) { Box<String> name = new Box<String>("corn"); Box<Integer> age = new Box<Integer>(712); Box<Number> number = new Box<Number>(314); //getUpperNumberData(name); getUpperNumberData(age); getUpperNumberData(number); } public static void getData(Box<?> data) { System.out.println("data :" + data.getData()); } public static void getUpperNumberData(Box<? extends Number> data) { System.out.println("data :" + data.getData()); } }
对于泛型,最主要的还是需要理解其背后的思想和目的。
目录代码带解析:
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wpydaguan/article/details/47300407