什么是类?在 Java 中是什么意思? [英] What does Class<?> mean in Java?
问题描述
我的问题如上所述.抱歉,这可能是重复的,但我找不到结尾带有 <?>
的示例.
My question is as above. Sorry, it's probably a duplicate but I couldn't find an example with the <?>
on the end.
为什么不直接使用 Class
作为参数?
Why would you not just use Class
as the parameter?
推荐答案
Class
是一个可参数化的类,因此你可以使用语法 Class
where T
是一种类型.通过编写 Class>
,您声明了一个 Class
对象,该对象可以是任何类型(?
是通配符).Class
类型是一种包含有关类的元信息的类型.
Class
is a parameterizable class, hence you can use the syntax Class<T>
where T
is a type. By writing Class<?>
, you're declaring a Class
object which can be of any type (?
is a wildcard). The Class
type is a type that contains meta-information about a class.
通过指定泛型类型的特定类型来引用泛型总是好的做法,通过使用 Class>
你尊重这种做法(你知道 Class
是可参数化的)但您并没有限制您的参数具有特定类型.
It's always good practice to refer to a generic type by specifying his specific type, by using Class<?>
you're respecting this practice (you're aware of Class
to be parameterizable) but you're not restricting your parameter to have a specific type.
关于泛型和通配符的参考:http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html
Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html
关于Class
对象和反射(Java语言用来自省的特性)的参考:https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html
Reference about Class
object and reflection (the feature of Java language used to introspect itself): https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html
这篇关于什么是类?在 Java 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!