Java 类类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/290982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:47:21  来源:igfitidea点击:

Java Class Type

javagenerics

提问by pn1 dude

I have a block of code that works and I wanted to ask what exactly is happening here?

我有一段有效的代码,我想问一下这里到底发生了什么?

Class<?> normalFormClass = null;

---added---

- -添加 - -

The wildcard "<?>" is the part that is confusing for me. Why would this be used rather than not using it (generics)?

通配符“ <?>”是让我感到困惑的部分。为什么要使用它而不是不使用它(泛型)?

回答by Vinko Vrsalovic

That means it can be a Class of any type. ? is a wildcard denoting the set of all types, or 'any'. So you can later do

这意味着它可以是任何类型的类。? 是一个通配符,表示所有类型的集合,或“任何”。所以你以后可以做

Integer normalForm = new Integer();
normalFormClass = normalForm.getClass();

or

或者

String normalForm = new String();
normalFormClass = normalForm.getClass();

If you are not aware of generics on Java, read http://java.sun.com/developer/technicalArticles/J2SE/generics/

如果您不了解 Java 上的泛型,请阅读http://java.sun.com/developer/technicalArticles/J2SE/generics/

As for the why, I think it might be to strictly express that you areusing generics everywhere and your code is not compatible with older Java versions, or maybe to shut up some trigger happy IDEs. And yes,

至于为什么,我认为可能是严格表达您任何地方使用泛型并且您的代码与旧Java版本不兼容,或者可能是关闭一些触发愉快的IDE。是的,

Class foo 

and

Class<?> foo 

are equivalent.

是等价的。

回答by John in MD

Also, the generic version

还有通用版

Class<?> normalClass = null;

is pretty much equivalent to the raw type version,

几乎等同于原始类型版本,

Class normalClass = null;

the main difference is that the latter will be compatible with versions of Java that do not support generics, like Java 1.4.

主要区别在于后者将与不支持泛型的 Java 版本兼容,例如 Java 1.4。

回答by Alex Miller

Class<T> means that since JDK 1.5, Class is generified by the type the class defines. One way to think about this is that Class is a factory for creating instances of its type.
Specifically, a Class<T> creates instances of type T from the Class.newInstance() method.

Class<T> 表示从 JDK 1.5 开始,Class 由类定义的类型泛化。考虑这一点的一种方法是 Class 是一个用于创建其类型实例的工厂。
具体来说, Class<T> 从 Class.newInstance() 方法创建类型 T 的实例。

From JDK 1.5 on, it is recommended by the Java Language Spec that you should not use raw types. So, it is highly recommended that when dealing with a Class instance of unknown type, you should refer to it as "Class<?>" and not just "Class". Of course, if you actually know the type or some bound, you might find some benefits of specifying it.

从 JDK 1.5 开始,Java 语言规范建议您不应使用原始类型。因此,强烈建议在处理未知类型的 Class 实例时,应将其称为“Class<?>”,而不仅仅是“Class”。当然,如果您确实知道类型或某些界限,您可能会发现指定它的一些好处。

回答by John Nilsson

Class or Class<?> works equally well. One reason to use the latter is to get rid of a few warnings the ide or compiler would throw at you when using the first form.

Class 或 Class<?> 工作得同样好。使用后者的一个原因是消除使用第一种形式时 ide 或编译器向您抛出的一些警告。

回答by Pyrolistical

I doubt this is homework, since I've never been in a class that talked about reflection.

我怀疑这是家庭作业,因为我从来没有上过一堂谈论反思的课。

But this is a simple null assignment. It doesn't do anything.

但这是一个简单的空赋值。它什么也不做。