简体   繁体   English

为什么必须在Java中使用.class来获取Class对象? 为什么不像Ruby中的类名呢?

[英]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. 我知道RubyJava非常不同。 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.

相关问题 为什么我必须使用java -cp。 (类名)而不是Java(类名)? - Why do I have to use java -cp . (class name) and not java (class name)? 为什么在 Java 实例化过程中要为类命名两次? - Why do you name the class twice during instantiation in Java? 为什么只为一个类使用 Java 接口? - Why use a Java Interface for just one class? 为什么在Java类中包含类成员变量不像ruby mixin? - Why is including a class member variable in a java class not like a ruby mixin? 为什么我不必导入我刚刚在我的主要 class 中使用它的 class? (爪哇) - Why don't I have to import a class I just made to use it in my main class? (Java) 为什么在Java中实例化新对象时必须两次键入类名称? - Why do we have to type the class name twice when instantiating a new object in Java? Java:为什么需要构造函数来使用父类中的对象? - Java: Why do you need constructors to use objects in a parent class? 为什么 C++ 使用指向链表中下一个节点的指针,但像 C# 或 Java 节点仅使用 C# 或 Java 的名称 - Why C++ uses pointer for next node in linked list, but langauges like C# or Java use just the name of the class Node? 您可以为Java程序使用类似“ HELLOWorld.class”的名称吗? - Can you use a name like “HELLOWorld.class” for a java program? 为什么某些类(例如java regex Pattern类)没有任何公共构造函数? - Why do some classes like the java regex Pattern class do not have any public constructors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM