Java - 比较类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6268384/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:06:59  来源:igfitidea点击:

Java - Comparing classes?

javaandroidclasscomparison

提问by aryaxt

How can i compare 2 classes?

我如何比较 2 个班级?

The following if statement never passes although class is type of MyClass:

尽管 class 是 MyClass 的类型,但以下 if 语句永远不会通过:

public void(Class class) {
   if (class == MyClass.class){

   }
}

回答by Carcamano

if (clazz.equals(MyClass.class)) {

}

BTW, class is a reserved word.

顺便说一句,类是一个保留字。

回答by Mike Samuel

To test whether clazzis a (sub) type of MyClassdo

测试是否clazz是(子)类型的MyClassdo

MyClass.class.isAssignableFrom(clazz)

From the javadoc for Class.isAssignableFrom

来自 javadoc Class.isAssignableFrom

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。如果是,则返回 true;否则返回false。如果此 Class 对象表示原始类型,则如果指定的 Class 参数正是此 Class 对象,则此方法返回 true;否则返回false。

具体来说,此方法测试是否可以通过标识转换或通过扩展引用转换将指定的 Class 参数表示的类型转换为此 Class 对象表示的类型。有关详细信息,请参阅 Java 语言规范的第 5.1.1 和 5.1.4 节。

So

所以

Object.class.isAssignableFrom(String.class)

is true because each Stringis also an Objectbut

是真的,因为每个String也是一个Object但是

String.class.isAssignableFrom(Object.class)

is false because not all Objects are Strings.

是假的,因为并非所有的Objects 都是Strings。



The name "isAssignableFrom" comes from the fact that,

isAssignableFrom”这个名字来自这样一个事实,

Class1 x = (Class2) null;

is only legal when

只有在合法的时候

Class1.class.isAssignableFrom(Class2.class)

I.e., we can assigna field or variable with static type Class1a value that comes froman expression whose static type is Class2.

即,我们可以指定字段或变量静态类型Class1到来的值它的静态类型是一个表达式Class2

回答by Matt Ball

You can use ==or .equals()to compare Classobjects.

您可以使用==.equals()来比较Class对象。

Example:

例子:

class MyClass
{
    public static void main (String[] args) throws java.lang.Exception
    {
        MyClass m = new MyClass();
        if (MyClass.class == m.getClass())
        {
            System.out.println("it worked");
        }
    }
}

Demo: http://ideone.com/AwbNT

演示:http: //ideone.com/AwbNT

回答by Genzer

You can use instanceofoperator to check if an instance belongs to a specific class or its subclasses.

您可以使用instanceof运算符检查实例是否属于特定类或其子类。

class MyClass{}

class SubClass extends MyClass{}

public static void main(String args[]) {

    SubClass object = new SubClass();

    if (object instanceof MyClass) {
        System.out.println("It works, too");
    }
}