简体   繁体   English

java中的Class clazz和Class <?> clazz有什么区别?

[英]What is the difference between Class clazz and Class<?> clazz in java?

I need to make use of reflection in java. 我需要在java中使用反射。 I understand that Class clazz creates a variable representing a Class object. 我知道Class clazz创建了一个表示Class对象的变量。 However, I am trying to reference a Class object from a String using the forName("aClassName") method. 但是,我试图使用forName("aClassName")方法从String引用Class对象。 My IDE (Eclipse), seems to prefer the notation Class<?> clazz for declaring the variable. 我的IDE(Eclipse)似乎更喜欢用符号Class<?> clazz来声明变量。 I have seen this notation many times elsewhere. 我在其他地方多次看过这种表示法。 What does this mean? 这是什么意思?

Edit: Removed reference to ternary operator as it is not relevant to this question. 编辑:删除了对三元运算符的引用,因为它与此问题无关。

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> for some type Foo ,但我不知道是什么”。

Similarly you can have wildcards with bounds: 类似地,您可以使用带有边界的通配符:

  • Class<? extends InputStream> Class<? extends InputStream> means " Class<Foo> for some type Foo , but I don't know what so long as it's InputStream or a subclass" Class<? extends InputStream>对于某些类型的Foo意味着“ Class<Foo> ,但我不知道它是什么,只要它是InputStream或子类”

  • Class<? super InputStream> Class<? super InputStream> means " Class<Foo> for some type Foo , but I don't know what so long as it's InputStream or a superclass" Class<? super InputStream>对于某些类型的Foo意味着“ Class<Foo> ,但我不知道它是什么,只要它是InputStream或超类”

See also the Java Generics FAQ for a lot more 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在Java编程语言中添加了通用性,参见ACM会议论文集 - 面向编程,系统,语言和应用(OOPSLA 98),1998年10月。

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的泛型实现的一部分,并指出类的类型未指定,因为其他一些答案已经解释过了。

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. 但是,除了最简单的语句之外,最好避免使用三元运算符,以提高代码的可读性。

In generic types the wildcard ? 在通用类型中,通配符? means "whatever class" (so Class<?> is the same as just Class but as raw type correctly parametrized). 表示“无论什么类”(因此Class<?>Class相同,但原始类型正确参数化)。

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

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