站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题Eclipse3.1中体验J2SE5.0之泛型

Eclipse3.1中体验J2SE5.0之泛型

摘要: Eclipse3.1中体验J2SE5.0之泛型

泛型(Generics)简介

  J2SE 5.0中的最显著的变化之一是添加对泛型类型的支持. 在J2SE 1.4 以及之前的版本中, Java程序并不是类型安全的. 例如, Collection framework中定义的List, Map等容器类的元素都是Object类型, 即这个类包含的元素是Object对象. 使用这种方式实现的列表, 可以用来操作整数, 实数, 字符串或者任何对象类型. 例如

  清单1.1 类型不安全的代码示例

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
List stringList = new ArrayList();stringList.add("abcde");String str = (String)stringList.get(0);
</td></tr></table>

  这种方法实现的列表需要使用强制类型转换(又称显示造型), 因此不是类型安全的. 在上面这段代码种, 虽然变量名为stringList, 但是我们仍然可以把一个整型对象添加到这个队列中, 例如,

  清单1.2 类型不安全的代码示例

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
stringList.add(new Integer(5));
</td></tr></table>

  在这种情况下, 从字符列表中获取对象时, 强制类型转换就会导致运行时异常.

  清单1.3 类型不安全的代码示例

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
String str = (String)stringList.get(1); //runtime exception
</td></tr></table>

  泛型是Java迈向类型安全的一个重要步骤, 使用泛型可以构造出类型安全的代码.

  声明泛型

  所谓泛型是指类型参数化(parameterized types). Java是一种强类型的语言, 在J2SE 1.4以及以前的版本中, 我们在定义一个Java类, 接口或者方法的时候, 必须指定变量的类型. 在声明泛型类、接口或者函数时, 定义变量的时候不指定某些变量的具体类型, 而是用一个类型参数代替. 在使用这个类, 接口, 或者方法的时候, 这个类型参数由一个具体类型所代替.

  2.1 泛型类

  下面的例子中介绍了如何创建一个最简单泛型类

  清单2.1, 最简单的泛型类

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public class GenSample {}
</td></tr></table>

  类名后面带有表明了这个类是泛型类, 其中T被成为类型参数(type parameter), 在使用泛型的时候, 类型参数可以被替换为任何的类类型, 但是不能是原始类型(primitive type), 例如int, double.

  下面通过一个列表的例子来具体说明如果声明泛型类和类型参数的用法.

  清单2.2, 泛型列表

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public class GenList {private T[] elements;private int size = 0;private int length = 0;public GenList(int size) {elements = (T[])new Object[size];this.size = size;}public T get(int i) {if (i < length) {return elements[i];}return null;}public void add(T e) {if (length < size - 1)elements[length++] = e;}}
</td></tr></table>

  在列表的例子中, 类型参数T被用来表示列表中的元素的类型, 即, 这个列表中的元素是T类型的.

  在使用这个列表时, 这个类型参数T会被具体的类型所替代.

  注意, 由于T时类型参数不是具体的类, 所以不能使用new操作符创建T的对象,例如new T(), 或者, new T[10].

  2.2 泛型接口

  在J2SE 5.0中, 不仅仅可以声明泛型类, 也可以声明泛型接口, 声明泛型接口和声明泛型类的语法类似, 也是在接口命称后面加上. 例如,

  清单3.2.3, 泛型接口
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public interface GenInterface {    void func(T t);}
</td></tr></table>

  2.3、声明多个类型参数的泛型类或者接口

  在声明泛型类的时候, 可是使用多个类型参数. 多个类型参数之间用逗号分开, 例如,

  清单3.2.4, 多个类型参数的泛型类
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public class GenMap {}
</td></tr></table>

  Eclipse 3.1的类创建向导支持创建泛型类, 如下图所示,

  图2.1 使用类向导创建泛型类

  使用Eclipse接口向导创建泛型接口

  图2.2 使用接口向导创建泛型接口

  2.4 泛型方法

  类型参数(type parameter)不仅仅可以用来声明泛型类或者泛型接口, 也可以用来声明泛型方法, 而且这种声明的泛型方法可以用在非泛型类中, 声明泛型方法的一般格式是

  清单2.5 泛型方法的一般形式
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
 return-type method-name(parameter-list) {}
</td></tr></table>
  清单3.2.6 泛型方法示例
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public  String getString(T obj) {return obj.toString();}
</td></tr></table>

  2.5 受限泛型

  受限泛型是指类型参数的取值范围是受到限制的. extends关键字不仅仅可以用来声明类的继承关系, 也可以用来声明类型参数(type parameter)的受限关系.例如, 我们只需要一个存放数字的列表, 包括整数(Long, Integer, Short), 实数(Double, Float), 不能用来存放其他类型, 例如字符串(String), 也就是说, 要把类型参数T的取值泛型限制在Number极其子类中.在这种情况下, 我们就可以使用extends关键字把类型参数(type parameter)限制为数字,

  清单2.7 受限泛型示例
<table style="WIDTH: 964px; HEIGHT: 127px" cellspacing="0" cellpadding="5" width="964" bgcolor="#cccccc" border="1"><tr><td>
public class Limited {public static void main(String[] args) {Limited number;   //正确Limited str;       //编译错误}}
</td></tr></table>

  在Eclipse 3.1中, 上例中的编译错误信息如下图所示

  图2.3 由于受限泛型导致的编译错误

  在程序中使用泛型

  3.1在程序中使用泛型类

  在创建泛型类的对象的时候, 和创建普通对象基本类似, 必须提供具体的类类型来替代类型参数T (J2SE 5.0目前不支持原始类型作为类型参数(type parameter)).

  清单3.1, 使用泛型类
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
//如果需要整型的列表GenList integerList = new GenList();//如果需要字符型的列表GenList strList = new GenList();//不能使用原始类型GenList nList = new GenList();  //编译错误
</td></tr></table>

  3.2 使用泛型解决类型安全性问题

  使用泛型实现的列表是类型安全的, 下列破坏类型安全语句会在编译的时候检查出来。 把鼠标放在错误标记上, Eclipse 3.1中的错误提示就会显示,如下图所示:

  图3.3.2 破坏类型安全引起的错误

  3.3 二义性错误

  GenMap在声明是使用了2个类型参数T和V, 因此在创建GenMap的对象的时候也需要提供2个具体的类类型来替代这2个类型参数, 例如,

  清单9, 多个参数的泛型类
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
GenMap gm = new GenMap();GenMap gm2 = new GenMap();
</td></tr></table>

  上例中, T和V虽然看起来是两个不同的类型参数, 但是在使用这个泛型类的时候, T和V很有可能被替换成同一种类型. 因此在声明多个类型参数的泛型类时, 要注意避免这种二义性错误, 例如,

  清单10, 二义性错误
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public class GenMap {    //编译错误, 二义性错误    public void set(T t){}    public void set(V v){}}
</td></tr></table>

  在上面这段代码, 如果T和V被替换成同一种类型, set函数的签名(signature)就是完全一样的, 所以编译器会报告二义性错误. 正确的用法是声明2个不同名的方法, 例如,

  清单10, 二义性错误
<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
public class GenMap {    public void setKey(T t){}    public void setValue(V v){}}
</td></tr></table>

  图3.3 二义性错误

  3.4 使用通配符

  前面我们创建了泛型的列表, 如果我需要一个方法来处理泛型列表, 例如, 我们希望把列表中的每个元素都打印出来, 但是类型参数(type parameter)只能使用在声明一个泛型类的时候, 如果类型参数使用在函数定义里会导致编译错误。

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

public static void print(GenList list){}//编译错误
</td></tr></table>

  在这种情况下, 我们需要用另外一种方法来表示一个泛型类, 否则, 就可能需要书写多个print函数。

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

public static void print(GenList list){}public static void print(GenList list){}…public static void print(GenList list){}
</td></tr></table>

  J2SE 5.0中提供了泛型的通配符"?", "?"可以用来代替任何类型, 例如使用通配符来实现print方法。

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

public static void print(GenList<?> list) {}
</td></tr></table>

  泛型的一些局限型

  (1) 类型参数不能实例化, 例如,

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>
T t= new T();   //编译错误
</td></tr></table>

  (2) 不能实例化类型参数的数组

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

T[] ts= new T[10];   //编译错误
</td></tr></table>

  (3) 类的静态变量不能声明为类型参数类型

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

public class GenClass {     private static T t;    //编译错误}
</td></tr></table>

  (4) 泛型类不能继承自Throwable以及其子类

<table cellspacing="0" cellpadding="5" width="100%" bgcolor="#cccccc" border="1"><tr><td>

public GenExpection extends Exception{}    //编译错误
</td></tr></table>

  泛型小结

  泛型是J2SE 5.0所提供的一项强大的功能, 使用泛型可以创建类型安全的、可重用的代码, 虽然目前Java的泛型还无法和C++的泛型相提并论, 但是, 随着Java语言本事的演进, 泛型会在Java语言中发挥更大的作用的.




↑返回目录
前一篇: Debian (Sarge) 下安装 J2SE 1.5.0.05
后一篇: Eclipse3.1中体验J2SE 5.0之枚举类型