[英]Why do you have to use .class in Java to get the Class object? Why not just the class name like in Ruby?
Why do you have to use .class
in Java to get the Class
object? 为什么必须在Java中使用
.class
来获取Class
对象? Why not just the class name like in Ruby? 为什么不像Ruby中的类名呢?
I know Ruby
and Java
are very different. 我知道
Ruby
和Java
非常不同。 But it seems counter-intuitive to actually have to type out .class
when you have already typed out the class name. 但是,当您已经输入类名时,实际上必须键入
.class
似乎是违反直觉的。
If there are static methods on the class, then you type MyClass.staticMethod()
, not MyClass.class.staticMethod()
. 如果类上有静态方法,则键入
MyClass.staticMethod()
,而不是MyClass.class.staticMethod()
。
This might be a pointless question, but I wanted to see if there was anyone out there that could enlighten me. 这可能是一个毫无意义的问题,但我想知道是否有人可以启发我。
Because classes and variables do not share a namespace in Java, which means you can have variables which have the same name as an existing class. 因为类和变量不在Java中共享命名空间,这意味着您可以拥有与现有类同名的变量。 This statement:
这个说法:
String Integer = "Integer";
is completely legal and creates a new String variable named Integer
even though there is a class Integer
. 是完全合法的,并创建一个名为
Integer
的新String变量,即使有一个类Integer
。
An example which shows that using the class name as a Class object would be ambiguous: 一个示例显示使用类名作为Class对象将是不明确的:
Class Integer = "Integer".getClass();
System.out.println(Integer.getName());
Would this print "java.lang.Integer" or "java.lang.String"? 这会打印“java.lang.Integer”还是“java.lang.String”?
But what if the variable Class Integer
would simply shadow the definition of the class Integer
? 但是,如果变量
Class Integer
只会遮蔽的定义class Integer
?
Do you consider shadowing a good thing? 你认为阴影是好事吗? It often creates confusion and nasty surprises.
它经常造成混乱和令人讨厌的惊喜。 But this is of course debatable.
但这当然值得商榷。 Also,
new Integer(42)
(constructor String(int) is undefined) now becomes a runtime error instead of a compile-time error. 此外,
new Integer(42)
(构造函数String(int)未定义)现在变为运行时错误而不是编译时错误。
But what if we would simply forbid to name variables after existing classes? 但是,如果我们只是禁止在现有类之后命名变量呢?
There are quite a lot of classes in the standard library. 标准库中有很多类。 That would really shrink down the namespace of available variable names.
这确实会缩小可用变量名称的命名空间。
But then what if we only forbid to name variables after classes which are import
ed in the current .java file? 但那么如果我们只禁止在当前.java文件中
import
类之后命名变量呢?
...and then you add a new import and your code doesn't compile anymore? ...然后你添加一个新的导入,你的代码不再编译?
I think there are a couple of reasons. 我认为有几个原因。 The first was mentioned in the comment by peeskillet, where the compiler has to deal with instantiation of a new type of the object:
第一个在peeskillet的评论中提到,编译器必须处理新类型对象的实例化:
MyClass myClassInstance = new MyClass();
The other is that there is actually an object ( Class<?>
) which represents a class of a type - its not just "that class". 另一个是实际上有一个对象(
Class<?>
)代表一个类的类 - 它不仅仅是“那个类”。 This can be used as an argument to other methods, so you need a way to distinguish between an attempt to create a new instance (above) and a reference to the Class<?>
of the object. 这可以用作其他方法的参数,因此您需要一种方法来区分创建新实例的尝试(上面)和对象的
Class<?>
的引用。
Whether this was a good idea, or if it could be simplified is certainly up for debate :) Note that Java arose from strong roots in C/C++, so there are most certainly things within it that came from that line of thinking - there's always the possibility for "seemed like a good idea at the time" 这是一个好主意,还是可以简化,肯定有争议:)请注意,Java起源于C / C ++的强大根源,所以其中有很多东西都来自于这种思维方式 - 总是存在“当时看起来像个好主意”的可能性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.