简体   繁体   English

类是什么<?>在 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.抱歉,这可能是重复的,但我找不到结尾带有<?>的示例。

Why would you not just use Class as the parameter?为什么不直接使用Class作为参数?

Class is a parameterizable class, hence you can use the syntax Class<T> where T is a type. Class是一个可参数化的类,因此您可以使用语法Class<T>其中T是一种类型。 By writing Class<?> , you're declaring a Class object which can be of any type ( ? is a wildcard).通过编写Class<?> ,您声明了一个可以是任何类型的Class对象( ?是通配符)。 The Class type is a type that contains meta-information about a 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.通过指定泛型类型的特定类型来引用泛型类型总是很好的做法,通过使用Class<?>你尊重这种做法(你知道Class是可参数化的)但你没有限制你的参数有一个具体类型。

Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html关于泛型和通配符的参考: http : //docs.oracle.com/javase/tutorial/java/generics/wildcards.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 Class对象和反射(Java语言用来自省的特性)参考: https : //www.oracle.com/technetwork/articles/java/javareflection-1536171.html

This <?> is a beast.这个<?>是一头野兽。 It often leads to confusion and errors, because, when you see it first, then you start believing, <?> is a wildcard for any java type .它通常会导致混淆和错误,因为当您第一次看到它时,您就会开始相信<?>任何 java 类型的通配符。 Which is .. not true.这是......不是真的。 <?> is the unknown type , a slight and nasty difference. <?>未知类型,一个轻微而令人讨厌的区别。

It's not a problem when you use it with Class .当您将它与Class一起使用时,这不是问题。 Both lines work and compile:这两行都可以工作并编译:

Class anyType = String.class;
Class <?> theUnknownType = String.class;

But - if we start using it with collections, then we see strange compiletime errors:但是- 如果我们开始将它与集合一起使用,那么我们会看到奇怪的编译时错误:

List<?> list = new ArrayList<Object>();  // ArrayList<?> is not allowed
list.add("a String");                    // doesn't compile ...

Our List<?> is not a collection, that is suitable for just any type of object.我们的List<?>不是一个集合,它适合任何类型的对象。 It can only store one type: the mystic "unkown type".它只能存储一种类型:神秘的“未知类型”。 Which is not a real type, for sure.这肯定不是真正的类型。

It means your Class reference can hold a reference to any Class object.这意味着您的 Class 引用可以保存对任何 Class 对象的引用。

It's basically the same as "Class" but you're showing other people who read your code that you didn't forget about generics, you just want a reference that can hold any Class object.它基本上与“Class”相同,但是您向其他阅读您的代码的人展示了您没有忘记泛型,您只需要一个可以保存任何 Class 对象的引用。

Bruce Eckel, Thinking in Java: Bruce Eckel,用 Java 思考:

In Java SE5, Class<?> is preferred over plain Class, even though they are equivalent and the plain Class, as you saw, doesn't produce a compiler warning.在 Java SE5 中, Class<?> 比普通 Class 更受欢迎,即使它们是等效的,而且普通的 Class,如您所见,不会产生编译器警告。 The benefit of Class<?> is that it indicates that you aren't just using a non-specific class reference by accident, or out of ignorance. Class<?> 的好处是它表明您不是偶然或出于无知而使用非特定类引用。 You chose the non-specific version.您选择了非特定版本。

It's a generics literal.这是一个泛型文字。 It means that you don't know the type of class this Class instance is representing, but you are still using the generic version.这意味着您不知道此Class实例所代表的类的类型,但您仍在使用通用版本。

  • if you knew the class, you'd use Class<Foo> .如果你知道这个类,你会使用Class<Foo> That way you can create a new instance, for example, without casting: Foo foo = clazz.newInstance();这样你就可以创建一个新实例,例如,无需强制转换: Foo foo = clazz.newInstance();
  • if you don't use generics at all, you'll get a warning at least (and not using generics is generally discouraged as it may lead to hard-to-detect side effects)如果您根本不使用泛型,至少会收到警告(通常不鼓励不使用泛型,因为它可能导致难以检测的副作用)

In generics, an unknown type is represented by the wildcard character "?".在泛型中,未知类型由通配符“?”表示。 Read here for official example. 在这里阅读官方示例。

That means a Class with a type of anything (unknown).这意味着具有任何类型(未知)的类。

You should read java generics tutorial to get to understand it better您应该阅读 java 泛型教程以更好地理解它

It means, the Class reference type can hold any Class object which represents any type.这意味着,Class 引用类型可以包含代表任何类型的任何 Class 对象。 If JVM loads a type, a class object representing that type will be present in JVM.如果 JVM 加载一个类型,则表示该类型的类对象将出现在 JVM 中。 we can get the metadata regarding the type from that class object which is used very much in reflection package.我们可以从反射包中经常使用的类对象中获取有关类型的元数据。

Suppose you have aa class named "myPackage.MyClass".假设您有一个名为“myPackage.MyClass”的类。 Assuming that is in classpath, the following statements are equivalent.假设它在类路径中,以下语句是等效的。

Class<?> myClassObject = MyClass.class; //compile time check

Class<?> myClassObject = Class.forname("myPackage.MyClass"); //only runtime check

This works in a similar fashion if the Class<?> reference is in method argument as well.如果 Class<?> 引用也在方法参数中,这将以类似的方式工作。

Please note that the class "Class" does not have a public constructor.请注意,“Class”类没有公共构造函数。 So you cannot instantiate "Class" instances with "new" operator.所以你不能用“new”运算符实例化“Class”实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM