作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在泛型中
class A<T extends Number>
允许
但是
class A<T super Integer>
不允许
我不明白这一点。这听起来像是新手问题,但我深陷其中
最佳答案
引用 Java Generics: extends, super and wildcards explained :
The super bound is not allowed in class definition.
//this code does not compile !
class Forbidden<X super Vehicle> { }Why? Because such construction doesn't make sense. For example, you can't erase the type parameter with Vehicle because the class Forbidden could be instantiated with Object. So you have to erase type parameters to Object anyway. If think about class Forbidden, it can take any value in place of X, not only superclasses of Vehicle. There's no point in using super bound, it wouldn't get us anything. Thus it is not allowed.
关于java - 为什么在类级别不允许泛型中的 super 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411256/
我是一名优秀的程序员,十分优秀!