gpt4 book ai didi

java - 对象的通用下界

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:12 24 4
gpt4 key购买 nike

可以将通用参数绑定(bind)编码为:

public <T super Object> void someMethod(T t);

这种界限是否有效?

最佳答案

根据 JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4类型参数(< T extends Object > 术语)包括:

TypeParameter:
TypeVariable TypeBound*

TypeBound:
extends TypeVariable
extends ClassOrInterfaceType AdditionalBoundList*

AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound

AdditionalBound:
& InterfaceType

* = 可选

您看到TypeBound,只有extends 指定了用法。不幸的是,未指定使用 super 的下限类型参数。下限仅在通配符用法中指定 ( JLS#4.5.1 )

好问题,这让我深入研究了 JLS,我不知道为什么这没有在 java 中实现,只是没有指定。

关于java - 对象的通用下界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106899/

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