gpt4 book ai didi

java - 为什么在类级别不允许泛型中的 super 关键字

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:26 26 4
gpt4 key购买 nike

在泛型中

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com