最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

对象的泛型下界

SEO心得admin97浏览0评论
本文介绍了对象的泛型下界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

发布评论

评论列表(0)

  1. 暂无评论