Generics Types 泛型学习笔记 1
作者:冰云 icecloud(AT)sina.comBLOG:
时间:2004.02.15
版权声明: 本文由冰云完成,首发于CSDN,作者保留中文版权。未经许可,不得使用于任何商业用途。欢迎转载,但请保持文章及版权声明完整。如需联络请发邮件:icecloud(AT)sina.com |
Java 1.5 提供了泛型支持。前几天,Sun发布了一篇tutorial。以下是对该tutorial的学习笔记。
Generics in the Java Programming Language
Gilad Bracha
Febrary 16, 2004.
1 泛型编译后实际上会产生不同的类型声明
public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); } |
基本的调用
List myIntList = new LinkedList (); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next(); |
Note 1: 每个泛型声明仅编译一次,并且为每个不同类型产生一个单独的文件。就像旧的类一样。比如,可能会有List 类和List 类被产生。
原文:A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration.
编译后,相当于产生了下面的类
public interface IntegerList { void add(Integer x); Iterator iterator(); } |
2 List 不是List