English 中文(简体)
如果物体属于 Java[复制]类,检查
原标题:Check if an object belongs to a class in Java [duplicate]

是否容易核实某一物体属于某一类别? 例如,我可以这样做。

if(a.getClass() = (new MyClass()).getClass())
{
    //do something
}

但是,这要求每次在飞行中立即发射一个新的物体,但只能予以抛弃。 是否有更好的办法检查“a”属于“MyClass”类?

最佳回答

The instanceof keyword, as described by the other answers, is usually what you would want. Keep in mind that instanceof will return true for superclasses as well.

如果你想看到某一物体是某一类别的一个直接例子,你可以比较该类别。 可以通过<条码>植被()获取某一例的类别物体。 也可通过<条码>ClassName.qu自动进入特定类别。

例如:

if (a.getClass() == X.class) {
  // do something
}

在上述例子中,如果<代码>a 是X的例,而不是a是<代码>的子类例,则该条件即为真实情况。 X 。

相比之下:

if (a instanceof X) {
    // do something
  }

在<条码>中 例如,如果<代码>a 是X的例,或者a是<>X>>的<>下级>。

大部分时间是<代码>instanceof。

问题回答

如果你永远需要积极这样做,你可以采取以下措施:

boolean isInstance(Object object, Class<?> type) {
    return type.isInstance(object);
}

rel=“nofollow noreferer”>java.lang.Class<><>>>>>>, 点名:Class on any Object (returns thecode>Class which is an instance of), 或 您可使用字面字面字面代码(例如,Stem/>。 也有其他办法,通过反转录(http://code>Class本身是切入点)。

使用instanceof 运营商:

if(a instanceof MyClass)
{
    //do something
}

我同意使用<代码>instanceof。 前面已经提到过。

使用<代码>instanceof的另一个好处是,如果使用<编码>null,请参见instanceof of will Return false,而a.getClass(>>,将 throw上

履历

通常的做法是:

if (a instanceof A)

然而,在有些情况下,如在一般论据中<代码>A。

由于 Java的类型 era缩,以下数字集:

<A> boolean someMethod(Object a) {
    if (a instanceof A)
    ...
}

和以下的打字工作(将制作<>unchecked 警告):

<A> void someMethod(Object a) {
    try {
        A casted = (A)a;    
    } catch (ClassCastException e) {
         ...
    }
}

您可在操作时间投到<代码>A上,因为在操作时间,<代码>A基本上为<代码>Object。

这些案件的解决办法是使用<代码>Clas,而不是一般性论点:

void someMethod(Object a, Class<A> aClass) {
    if (aClass.isInstance(a)) {
       A casted = aClass.cast(a);
       ...
    }
}

之后,你可以把这种方法称作:

someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签