Java的Generics的几点限制

参见

http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html

To use Java generics effectively, you must consider the following restrictions:

  • Cannot Instantiate Generic Types with Primitive Types
  • Cannot Create Instances of Type Parameters
  • Cannot Declare Static Fields Whose Types are Type Parameters
  • Cannot Use Casts or instanceof With Parameterized Types
  • Cannot Create Arrays of Parameterized Types
  • Cannot Create, Catch, or Throw Objects of Parameterized Types
  • Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type


Cannot Instantiate Generic Types with Primitive Types

这个是说你不能用primitive type来做类型参数,就是说下面这个使用泛型的例子是不对的:
Java代码 复制代码 收藏代码
  1. class Pair<K, V> {
  2. private K key;
  3. private V value;
  4. public Pair(K key, V value) {
  5. this.key = key;
  6. this.value = value;
  7. }
  8. // ...
  9. }
  10. Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error



Cannot Create Instances of Type Parameters
这个是不能直接new T()来创建类型参数的实例。你必须用反射的技术来做到:
Java代码 复制代码 收藏代码
  1. public static <E> void append(List<E> list, Class<E> cls) throws Exception {
  2. E elem = cls.newInstance(); // OK
  3. list.add(elem);
  4. }

这里反射的技术就是传一个类作为参数。


Cannot Declare Static Fields Whose Types are Type Parameters

这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
Java代码 复制代码 收藏代码
  1. public class MobileDevice<T> {
  2. private static T os;
  3. // ...
  4. }
  5. MobileDevice<Smartphone> phone = new MobileDevice<>();
  6. MobileDevice<Pager> pager = new MobileDevice<>();
  7. MobileDevice<TabletPC> pc = new MobileDevice<>();

那么os的实际类型是什么呢?乱套了肯定。所以Java禁止这样做。

Cannot Use Casts or instanceof with Parameterized Types
因为java在编译是做了type erasure,所以无法区分ArrayList<Integer>和ArrayList<String>,所以Java不允许如下code编译通过:
Java代码 复制代码 收藏代码
  1. public static <E> void rtti(List<E> list) {
  2. if (list instanceof ArrayList<Integer>) { // compile-time error
  3. // ...
  4. }
  5. }

posted on 2013-02-18 20:10  蜜雪薇琪  阅读(251)  评论(0编辑  收藏  举报