Java的Generics的几点限制

本文详细介绍了Java泛型的使用限制,包括不能用原始类型做类型参数、不能直接创建类型参数实例、不能声明静态字段等,并通过具体示例进行说明。
参见

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

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

[list]
[*]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
[/list]

[color=red]Cannot Instantiate Generic Types with Primitive Types[/color]

这个是说你不能用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



[color=red]Cannot Create Instances of Type Parameters[/color]
这个是不能直接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);
}

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


[color=red]Cannot Declare Static Fields Whose Types are Type Parameters[/color]

这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
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禁止这样做。

[color=red]Cannot Use Casts or instanceof with Parameterized Types[/color]
因为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
// ...
}
}


[color=red]Cannot Create Arrays of Parameterized Types[/color]
也就是说下面这行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.


[color=red]Cannot Create, Catch, or Throw Objects of Parameterized Types[/color]
一个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
// ...
}
}

这些都会编译错误。

[color=red]Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type[/color]
这个是说因为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要在编译期解决所有类型检查的,所以这些能引起混乱的它肯定要禁掉了。参见[url=http://stackoverflow.com/questions/176446/how-can-elements-be-added-to-a-wildcard-generic-collection]stackoverflow上面的回答[/url]。
多说一点的是更详细的关于genercis的使用,参见
[url]http://tutorials.jenkov.com/java-generics/wildcards.html[/url],
关于? extends A的解释是List<? extends A>是只知道是一个list,可以装A或者A的子类,所以你可以读取里面的内容并cast成A, 但是不能insert。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值