[英]in Java syntax, Class<? extends Something>
Class<? extends Something>
Here's my interpretation, it's class template but the class ?这是我的解释,它是类模板,但是类 ? means the name of the class is undetermined and it extends the Something class.意味着类的名称未确定,它扩展了Something类。
if there's something wrong with my interpretation, let me know.如果我的解释有问题,请告诉我。
There are a few confusing answers here so I will try and clear this up.这里有一些令人困惑的答案,所以我会尝试澄清这一点。 You define a generic as such:您可以这样定义泛型:
public class Foo<T> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
If you want a generic on Foo to always extend a class Bar you would declare it as such:如果您希望 Foo 上的泛型始终扩展类 Bar 您可以将其声明为:
public class Foo<T extends Bar> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
The ?
?
is used when you declare a variable.在声明变量时使用。
Foo<? extends Bar>foo = getFoo();
OR或者
DoSomething(List<? extends Bar> listOfBarObjects) {
//internals
}
You are almost right.你几乎是对的。 Basically, Java has no concept of templates (C++ has).基本上,Java 没有模板的概念(C++ 有)。 This is called generics.这称为泛型。 And this defines a generic class Class<>
with the generics' attribute being any subclass of Something
.这定义了一个泛型类Class<>
,泛型的属性是Something
任何子类。
I suggest reading up " What are the differences between “generic” types in C++ and Java? " if you want to get the difference between templates and generics.如果您想了解模板和泛型之间的区别,我建议您阅读“ C++ 和 Java 中的“泛型”类型之间有什么区别? ”。
You're right你是对的
Definition is that the class has to be subtype of Something定义是该类必须是Something的子类型
It's the same as Class<T>
, but there is a condition that T
must extends Something
Or implements Something
as Anthony Accioly suggested它与Class<T>
相同,但有一个条件,即T
必须像 Anthony Accioly 建议的那样extends Something
或implements Something
It can also be class Something
itself它也可以是类Something
本身
You're correct.你是对的。
In Java generics, the ?
在 Java 泛型中, ?
operator means "any class".运算符的意思是“任何类”。 The extends
keyword may be used to qualify that to "any class which extends/implements Something
(or is Something
). extends
关键字可用于将其限定为“任何扩展/实现Something
(或为Something
)的类。
Thus you have "the Class
of some class, but that class must be or extend/implement Something
".因此,你有“ Class
的一些类,但这个类必须是或扩展/实现Something
”。
You're correct.你是对的。
However usually you will want to name the class that extends Something and write eg <E extends Something>
.然而,通常你会想要命名扩展Something的类并编写例如<E extends Something>
。 If you use ?
如果你使用?
you can't do anything with the given type later.你以后不能对给定的类型做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.