`
standalone
  • 浏览: 596122 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java的Generics的几点限制

    博客分类:
  • java
阅读更多
参见

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来做类型参数,就是说下面这个使用泛型的例子是不对的:
class Pair<K, V> {

    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    // ...
}

Pair<int, char> p = new Pair<>(8, 'a');  // compile-time error



Cannot Create Instances of Type Parameters
这个是不能直接new T()来创建类型参数的实例。下面的例子里,在run time,有关T或者E的类型信息都找不到了,所以你必须用反射的技术来做到:
T instantiateElementType(List<T> arg)
{
  return new T(); //causes a compilation error
}

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}

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


Cannot Declare Static Fields Whose Types are Type Parameters

这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
public class MobileDevice<T> {
    private static T os;

    // ...
}
MobileDevice<Smartphone> phone = new MobileDevice<>();
MobileDevice<Pager> pager = new MobileDevice<>();
MobileDevice<TabletPC> pc = new MobileDevice<>();

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

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


Cannot Create Arrays of Parameterized Types
也就是说下面这行code会编译错误。

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile error

为什么要禁止这个呢?先看不同类型的object插入到array里会怎么样:

Object[] strings = new String[2];
strings[0] = "hi";   // OK
strings[1] = 100;    // An ArrayStoreException is thrown.


换成generic list呢,假设下面第一行code能够编译通过,那么第三行code在运行时就无法抛出ArrayStoreException了。所以Java要禁掉这样写法。

Object[] stringLists = new List<String>[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.


Cannot Create, Catch, or Throw Objects of Parameterized Types
一个generic的class不能继承Throwable(间接或直接都不行), 不能catch类型参数的实例(但可以throw)。例如:

// Extends Throwable indirectly
class MathException<T> extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException<T> extends Throwable { /* ... */ // compile-time error


public static <T extends Exception, J> void execute(List<J> jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}

这些都会编译错误。

Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
这个是说因为type erasure的原因,会导致像
public class Example {
    public void print(Set<String> strSet) { }
    public void print(Set<Integer> intSet) { }
}

两个print方法完全一样,所以也被禁止。

以上是java文档上列出来的,还有一种情况:
 public List<? extends Foo> getFoos()
  {
    List<? extends Foo> foos = new ArrayList<? extends Foo>();
    foos.add(new SubFoo());
    return foos;
  }

第三行会编译错误“Cannot instantiate the type ArrayList<? extends Foo>", 原因是如果这行ok,那么编译器就不知道是否add一个SubFoo是安全的,如果foos被赋值成ArrayList<AltFoo> 呢?记住,compiler要在编译期解决所有类型检查的,所以这些能引起混乱的它肯定要禁掉了。参见stackoverflow上面的回答
多说一点的是更详细的关于genercis的使用,参见
http://tutorials.jenkov.com/java-generics/wildcards.html
关于? extends A的解释是List<? extends A>是只知道是一个list,可以装A或者A的子类,所以你可以读取里面的内容并cast成A, 但是不能insert。
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics