有什么区别?和T在类和方法签名? [英] what is the difference between ? and T in class and method signatures?
问题描述
public interface ArrayOfONEITEMInterface< T extends ONEITEMInterface> {
public List< T> getONEITEM();
}
编译但不是
public interface ArrayOfONEITEMInterface <?扩展ONEITEMInterface> {
public List<?> getONEITEM();
}
有什么区别?和T中的类和方法签名?
?
是通配符并且意味着 ONEITEMInterface
的任何子类包括其自身。
$ b
因为 第一种场景 >表示整个类可以正确处理一个类型的 第二种场景允许每个实例可在任何子类型 T
是 ONEITEMInterface 的特定 code>在这种情况下。
?
是通配符,所以?
在你的方法声明中的类声明和?
中,因此它不会被编译。只需列表<?>
Bar
每个实例。
界面Foo< T延伸栏> {
列表< T>得到();
Bar
接口Foo {
列表< ;?扩展Bar> get()
}
why does
public interface ArrayOfONEITEMInterface <T extends ONEITEMInterface>{
public List<T> getONEITEM();
}
compile, but not
public interface ArrayOfONEITEMInterface <? extends ONEITEMInterface>{
public List<?> getONEITEM();
}
what is the difference between ? and T in class and method signatures?
?
is a wildcard and means any sublass of ONEITEMInterface
including itself.
T
is a specific implementation of ONEITEMInterface
in this case.
Since ?
is a wildcard, there is no relation between your ?
in the class declaration and the ?
in your method declaration hence it won't compile. Just List<?> getONEITEM();
will compile though.
The first scenario means the entire class can handle exactly one type of Bar
per instance.
interface Foo<T extends Bar> {
List<T> get();
}
The second scenario allows each instance to operate on any subtype of Bar
interface Foo {
List<? extends Bar> get()
}
这篇关于有什么区别?和T在类和方法签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!