java java中的Class clazz和Class<?> clazz有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15144641/
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
What is the difference between Class clazz and Class<?> clazz in java?
提问by Andrew Harasta
I need to make use of reflection in java. I understand that Class clazz
creates a variable representing a Class
object. However, I am trying to reference a Class
object from a String
using the forName("aClassName")
method. My IDE (Eclipse), seems to prefer the notation Class<?> clazz
for declaring the variable. I have seen this notation many times elsewhere. What does this mean?
我需要在java中使用反射。我知道这Class clazz
会创建一个表示Class
对象的变量。但是,我正在尝试使用该方法Class
从 a引用一个对象。我的 IDE (Eclipse) 似乎更喜欢声明变量的符号。我在其他地方多次看到这个符号。这是什么意思?String
forName("aClassName")
Class<?> clazz
Edit: Removed reference to ternary operator as it is not relevant to this question.
编辑:删除了对三元运算符的引用,因为它与此问题无关。
回答by Jon Skeet
Class
is a raw type- it's basically a generic type that you're treating as if you didn't know about generics at all.
Class
是原始类型- 它基本上是一种泛型类型,您将其视为根本不了解泛型。
Class<?>
is a generic type using an unbound wildcard- it basically means "Class<Foo>
for some type Foo
, but I don't know what".
Class<?>
是使用未绑定通配符的泛型类型- 它基本上意味着“Class<Foo>
对于某些类型Foo
,但我不知道是什么”。
Similarly you can have wildcards with bounds:
同样,您可以使用带边界的通配符:
Class<? extends InputStream>
means "Class<Foo>
for some typeFoo
, but I don't know what so long as it'sInputStream
or a subclass"Class<? super InputStream>
means "Class<Foo>
for some typeFoo
, but I don't know what so long as it'sInputStream
or a superclass"
Class<? extends InputStream>
意思是“Class<Foo>
对于某种类型Foo
,但我不知道它是什么InputStream
或子类”Class<? super InputStream>
意思是“Class<Foo>
对于某种类型Foo
,但我不知道它是什么InputStream
或超类”
See also the Java Generics FAQfor a lotmore information:
又见Java泛型常见问题解答了很多的详细信息:
And the Java Language Specification:
和 Java 语言规范:
In particular, from the raw types section:
特别是,从原始类型部分:
Raw types are closely related to wildcards. Both are based on existential types. Raw types can be thought of as wildcards whose type rules are deliberately unsound, to accommodate interaction with legacy code. Historically, raw types preceded wildcards; they were first introduced in GJ, and described in the paper Making the future safe for the past: Adding Genericity to the Java Programming Language by Gilad Bracha, Martin Odersky, David Stoutamire, and Philip Wadler, in Proceedings of the ACM Conference on Object-Oriented Programming, Systems, Languages and Applications (OOPSLA 98), October 1998.
原始类型与通配符密切相关。两者都基于存在类型。原始类型可以被认为是通配符,其类型规则故意不合理,以适应与遗留代码的交互。从历史上看,原始类型在通配符之前;它们首先在 GJ 中被介绍,并在 Gilad Bracha、Martin Odersky、David Stoutamire 和 Philip Wadler 在 ACM 对象会议论文集的论文“使未来安全为过去:为 Java 编程语言添加通用性”中描述。面向编程、系统、语言和应用程序 (OOPSLA 98),1998 年 10 月。
回答by yogaphil
The first thing to realize is that, in this case, the "?" is NOT the ternary operator, but is part of Java's generics implementation and indicates that the type of Class is unspecified, as some of the other answers have already explained.
首先要意识到的是,在这种情况下,“?” 不是三元运算符,而是 Java 泛型实现的一部分,并指示 Class 的类型未指定,正如其他一些答案已经解释的那样。
To clarify the question about the ternary operator, it is actually very simple.
澄清三元运算符的问题,其实很简单。
Imagine you have the following if statement:
假设您有以下 if 语句:
boolean correct = true;
String message;
if (correct) {
message = "You are correct.";
} else {
message = "You are wrong.";
}
You can rewrite that with the ternary operator (think of it as the if-else-shortcut operator):
您可以使用三元运算符重写它(将其视为 if-else-shortcut 运算符):
message = (correct) ? "You are correct." : "You are wrong.";
However, it's best to avoid the ternary operator for all but the simplest statements in order to improve the readability of your code.
但是,除了最简单的语句外,最好避免使用三元运算符,以提高代码的可读性。
回答by Vinze
In generic types the wildcard ?
means "whatever class" (so Class<?>
is the same as just Class
but as raw type correctly parametrized).
在泛型类型中,通配符?
意味着“任何类”(因此Class<?>
与 just 相同,Class
但原始类型正确参数化)。