Java 比较 Class 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2647257/
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
Comparing Class objects
提问by reto
I have to compare a Classobject against a list of pre-defined classes.
我必须将Class对象与预定义的类列表进行比较。
Is it safe to use ==or should I use equals?
使用安全==还是应该使用equals?
if (klass == KlassA.class) {
} else if (klass == KlassB.class) {
} else if (klass == KlassC.class) {
} else {
}
Note:I cannot use instanceof, I don't have an object, I just have the Classobject. I (mis)use it like an enum in this situation!
注意:我不能使用instanceof,我没有对象,我只有Class对象。在这种情况下,我(错误地)像枚举一样使用它!
采纳答案by robinst
java.lang.Classdoes notoverride the equalsmethod from java.lang.Object, which is implemented like this:
java.lang.Class不会覆盖equalsfrom的方法java.lang.Object,其实现方式如下:
public boolean equals(Object obj) {
return (this == obj);
}
So a == bis the same as a.equals(b)(except if ais null).
所以a == b与a.equals(b)(除非a为空)相同。
回答by Paulo Santos
It's probably safe.
应该是安全的。
If the object doesn't override the Equalsmethod it will make a comparison between the references. And if two variables point to the same object, their references match.
如果对象没有覆盖该Equals方法,它将在引用之间进行比较。如果两个变量指向同一个对象,则它们的引用匹配。
回答by Jonhnny Weslley
For the most of the Java applications this is correct. However, comparing Java classes using the operator == is safe just if both the classes are loaded by the same classloader.
对于大多数 Java 应用程序来说,这是正确的。但是,仅当两个类都由同一个类加载器加载时,使用运算符 == 比较 Java 类是安全的。
回答by jt.
I am not sure if this will work for your specific situation, but you could try Class.isAssignableFrom(Class).
我不确定这是否适用于您的特定情况,但您可以尝试Class.isAssignableFrom(Class)。
KlassA.class.isAssignableFrom(klass)
回答by polarfish
I prefer to use == for comparison between class objects and enum constants because it results in compilation time errors in case of incompatible types.
我更喜欢使用 == 来比较类对象和枚举常量,因为它会在类型不兼容的情况下导致编译时错误。
For example:
例如:
Class<?> cls1 = Void.class;
String cls2 = "java.lang.String";
if (cls1 == cls2) doSomething(); // Won't compile
if (cls1.equals(cls2)) doSomething(); // Will compile
回答by MMKarami
As mentioned in previous answers, to compare objects of Class type (or java.lang.Class objects) we should use == operator. However, It may be a bit confusing because always the result of comparison between objects through == operator can not cause right results (we usually use equal() method). For example, the result of this expression is false:
正如前面的答案中提到的,要比较 Class 类型的对象(或 java.lang.Class 对象),我们应该使用 == 运算符。但是,这可能有点令人困惑,因为总是通过 == 运算符进行对象之间的比较结果无法产生正确的结果(我们通常使用 equal() 方法)。例如,这个表达式的结果是假的:
new String("book") == new String("book")//false
The reason is that,
原因是,
The virtual machine manages a unique Class object for each type. Therefore, you can use the == operator to compare java.lang.Class objects. From Core Java for the Impatient - Page 153
虚拟机为每种类型管理一个唯一的 Class 对象。因此,您可以使用 == 运算符来比较 java.lang.Class 对象。来自为不耐烦的核心 Java - 第 153 页
Therefore:
所以:
new String("book").getClass() == new String("book").getClass()//true
or
或者
Class.forName("java.lang.String") == Class.forName("java.lang.String")//true
result in true.
结果为真。

