java是否支持Foo< T super X>如果不是,为什么? [英] Does java support class Foo<T super X> ,if no,why?

查看:106
本文介绍了java是否支持Foo< T super X>如果不是,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java是否支持 class Foo< T super X> ,如果不是,为什么?

一个类型参数,X是一个类型。

解决方案

在Generics上下文中的 super 关键字只能与通配符()结合使用, )变量或方法参数(消费者),这些变量或方法参数是一些Generic类型。



例如,这些是有效的:

 列表与LT ;?超级东西> list = someListReference; 
$ b $ public void methodThatPopulates AList(List< ;? super Something> consumer){
...
list.add(new Something());
...
}

更多信息:




Does java support class Foo<T super X> ,if no,why ?

Where T is a type parameter and X is a type.

解决方案

No. The super keyword in the context of Generics could only be used in combination with a wildcard (?) when declaring (consumer) variables or method parameters (consumers) that are of some Generic type.

For example, these are valid:

List<? super Something> list = someListReference;

public void methodThatPopulatesAList(List<? super Something> consumer) {
    ...
    list.add(new Something());
    ...
}

More info:

这篇关于java是否支持Foo&lt; T super X&gt;如果不是,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆