对象的泛型下界

编程入门 行业动态 更新时间:2024-10-15 14:19:29
本文介绍了对象的泛型下界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可以将一个泛型参数绑定为:

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

是否有这样一个绑定的有效用法?

解决方案

根据JLS docs.oracle/javase/specs/jls/se7/html/jls-4.html#jls-4.4 类型参数(< T

TypeParameter: TypeVariable TypeBound * TypeBound: extends TypeVariable extends ClassOrInterfaceType AdditionalBoundList * AdditionalBoundList: AdditionalBound AdditionalBoundList AdditionalBound AdditionalBound:&接口类型

* =可选

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

好的问题,让我在JLS中挖掘,我不知道为什么没有实现在java中,它只是没有指定。

It is possible to code a generic parameter bound as:

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

Is there a valid usage of such a bound?

解决方案

According to the JLS docs.oracle/javase/specs/jls/se7/html/jls-4.html#jls-4.4 a type parameter (The < T extends Object > term) consists out of:

TypeParameter: TypeVariable TypeBound* TypeBound: extends TypeVariable extends ClassOrInterfaceType AdditionalBoundList* AdditionalBoundList: AdditionalBound AdditionalBoundList AdditionalBound AdditionalBound: & InterfaceType

* = optional

You see the TypeBound, there is only the usage of extends specified. Unfortunately, a lower bound type parameter, using super, is not specified. A lower bound is only specified in the wildcard usage (JLS#4.5.1)

Good question, that got me digging in the JLS, and I don't know why this not implemented in java, it is just not specified.

更多推荐

对象的泛型下界

本文发布于:2023-11-30 17:02:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下界   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!