问题描述
Class 与a Class 声明之间有什么区别。
- 类别a;
- 类<?> 原始类型:
- Class a;
- Class<?> b;
类//未知类(原始类型)
Class<?> //未知类(通用版本)
Class< String> // The String class
在这种特殊情况下, Class 和 Class ,因为它们都表示一个未知的类。根据现有的声明,编译器可以要求泛型类型而不是原始类型。
但是:从Java 1.5开始,您应该使用generic尽可能的形式。 Class 明确指出您的意思是未知类, Class< String> 意味着字符串类。原始的 Class 可能意味着 。 最后它对编译器没有什么区别,但它使代码的意图更容易理解和维护,这是非常不同的。 What is the difference between a Class and a Class<?> declaration.
It's the same as with all generic and raw types:
Class // An unknown class (raw type) Class<?> // An unknown class (generic version) Class<String> // The String class
In this special case there's no much practical difference between Class and Class<?> because they both denote an unknown class. Depending on the existing declarations the compiler can demand a generic type instead of a raw type.
But: Since Java 1.5 you should use the generic form wherever possible. Class<?> clearly states that you mean "an unknown class", Class<String> cleary states that you mean the String class. A raw Class could mean both.
In the end it makes not much of a difference to the compiler but it makes a huge difference in making the intentions of your code more understandable and maintainable.
这篇关于类和类之间的区别<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!