JDK1.5新特性-泛型

874阅读 0评论2009-10-22 sytrydor
分类:Java

泛型:
支持定义带有抽象类型参数的类,这些参数由您在实例化时指定。泛型为提高大型程序的类型安全和可维护性带来了很大的潜力。
泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。
泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,提高代码的重用率。

泛型—集合类使用:
List lstQC = new ArrayList();

Map objUserMap = new HashMap(100);

public int queryCount(List condition){}

public List queryList(List condition, int pageId, int size)

泛型—类使用:
Class
 public interface IReceiveVO {
    /**
     * 转换记录成VO
     * @param rs ResultSet
     * @return 模块VO
     * @throws SQLException 异常
     */
    E receiveVO(ResultSet rs) throws SQLException;
}

使用
 class DeptReceiveVO implements IReceiveVO {
  public DeptVO receiveVO(ResultSet rs) throws SQLException {...}
 }

protected List getList(IReceiveVO receive, final String sql,List params, int pageId, int size)

泛型—方法使用:
protected List getList(IReceiveVO receive, final String sql,List params, int pageId, int size)
泛型类和泛型方法
public interface Collection extends Iterable {
    Iterator iterator();
    T[] toArray(T[] a);
    boolean add(E o);
    boolean containsAll(Collection c);
    boolean addAll(Collection c);
    boolean removeAll(Collection c);
    boolean retainAll(Collection c);
}
泛型—还有更多:
更复杂
class?GenericType3?implements?Comparable?{ ?? private?GenericType2?a; ??
??private?GenericType2,?List>>?b; ??public?int?compareTo(GenericType3?g)?{。。。} ??
} ??
class?GenericType4{ ??
??public?GenericType3,?A,?A>?max(List>?list)?{ ?? ?return?null; ?? ??} ??
}
影响更多面
 对equals的影响等

上一篇:李开复: 创业不必依靠好点子
下一篇:java泛型