Java - 是否有一个“子类”?像实例?

发布于 2024-08-30 17:51:08 字数 113 浏览 13 评论 0原文

我重写了 equals() 方法,我需要知道该对象是否是 Event 子类的实例(Event 是超类)。我想要类似“obj subclassof Event”的东西。这是怎么做到的呢?

提前致谢!

Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

聊慰 2024-09-06 17:51:08

instanceof 可以很好地处理这个问题。

instanceof can handle that just fine.

情绪 2024-09-06 17:51:08

使用以下代码,您可以检查对象是否是扩展 Event 的类,但本身不是 Event 类实例。

if(myObject instanceof Event && myObject.getClass() != Event.class) {
    // then I'm an instance of a subclass of Event, but not Event itself
}

默认情况下,instanceof 检查对象是否属于任何事件级别的指定类或子类(扩展或实现)。

With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself.

if(myObject instanceof Event && myObject.getClass() != Event.class) {
    // then I'm an instance of a subclass of Event, but not Event itself
}

By default instanceof checks if an object is of the class specified or a subclass (extends or implements) at any level of Event.

长亭外,古道边 2024-09-06 17:51:08

确实 instanceof 应该足够好,但如果你想确定该类确实是一个子类,那么你可以这样提供检查:

if (object instanceof Event && object.getClass() != Event.class) {
    // is a sub-class only
}

由于 Adrian 比我领先一点,我也会添加一种可以使用通用方法来执行此操作的方法。

public static boolean isSubClassOnly(Class clazz, Object o) {
    return o != null && clazz.isAssignableFrom(o) && o.getClass() != clazz;
}

通过以下方式使用它:

if (isSubClassOnly(Event.class, object)) {
    // Sub-class only
}

Really instanceof ought to be good enough but if you want to be sure the class is really a sub-class then you could provide the check this way:

if (object instanceof Event && object.getClass() != Event.class) {
    // is a sub-class only
}

Since Adrian was a little ahead of me, I will also add a way you could do this with a general-purpose method.

public static boolean isSubClassOnly(Class clazz, Object o) {
    return o != null && clazz.isAssignableFrom(o) && o.getClass() != clazz;
}

Use this by:

if (isSubClassOnly(Event.class, object)) {
    // Sub-class only
}
烙印 2024-09-06 17:51:08

您可能想查看 someObject.getClass().isAssignableFrom(otherObject.getClass());

You might want to look at someObject.getClass().isAssignableFrom(otherObject.getClass());

森林很绿却致人迷途 2024-09-06 17:51:08

Java中没有直接的方法来检查子类。
instanceof Event 将为任何子类对象返回 true

您可以在该对象上执行 getClass() ,然后使用 getSuperclass() 方法Class 对象来检查超类是否为 Event

There is no direct method in Java to check subclass.
instanceof Event would return back true for any sub class objects

The you could do getClass() on the object and then use getSuperclass() method on Class object to check if superclass is Event.

掩耳倾听 2024-09-06 17:51:08

如果 obj 是 Event 的子类,那么它是一个 instanceof。 obj 是它派生的每个类/接口的实例。所以至少所有对象都是 Object 的实例。

If obj is a subclass of Event then it is an instanceof. obj is an instanceof every class/interface that it derives from. So at the very least all objects are instances of Object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文