Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96255
  • 博文数量: 54
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-30 00:36
文章分类

全部博文(54)

文章存档

2010年(1)

2009年(52)

2008年(1)

我的朋友

分类: Java

2009-10-22 17:35:28

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

泛型—集合类使用:
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的影响等

阅读(785) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~