Class<?>是什么用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< T>
其中T是一个类型。通过编写 Class<?>
,您将声明一个 Class
对象,该对象可以是任何类型(?
是一个通配符)。 类
类型是包含有关类的元信息的类型。
Class
is a parametrizable 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 metainformation 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 parametrizable) 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
关于类对象和反射的引用(Java的特性)用于内省的语言): http://java.sun.com/developer/technicalArticles / ALT / Reflection /
Reference about Class object and reflection (the feature of Java language used to introspect itself): http://java.sun.com/developer/technicalArticles/ALT/Reflection/
这篇关于Class<?>是什么用Java表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!