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
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()来创建类型参数的实例。你必须用反射的技术来做到:
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. }
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

这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
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<>();
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编译通过:
Java代码 复制代码 收藏代码
  1. public static <E> void rtti(List<E> list) {
  2. if (list instanceof ArrayList<Integer>) { // compile-time error
  3. // ...
  4. }
  5. }
public static <E> void rtti(List<E> list) {
    if (list instanceof ArrayList<Integer>) {  // compile-time error
        // ...
    }
}

转载于:https://www.cnblogs.com/shhaoran/archive/2013/02/18/2924401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值