如何确定两个参数是否是同一但未知类的实例?

发布于 2024-08-29 02:31:47 字数 760 浏览 1 评论 0原文

假设我们有一个方法,它接受两个 Object 类型的参数 o1 和 o2 并返回一个布尔值。我希望此方法仅当参数是同一类的实例时才返回 true,例如:

foo(new Integer(4),new Integer(5));

应该返回 true,但是:

foo(new SomeClass(), new SubtypeSomeClass());

应该返回 false 以及:

foo(new Integer(3),"zoo");

应该返回 false。

我相信一种方法是比较完全限定的类名:

public boolean foo(Object o1, Object o2){
 Class<? extends Object> c1 = o1.getClass();
 Class<? extends Object> c2 = o2.getClass();
 if(c1.getName().equals(c2.getName()){ return true;}
 return false;  
}

另一种条件语句是:

if (c1.isAssignableFrom(c2) && c2.isAssignableFrom(c1)){ return true; }

后一种选择相当慢。这个问题还有其他替代方案吗?

Let us say we have a method which accepts two arguments o1 and o2 of type Object and returns a boolean value. I want this method to return true only when the arguments are instances of the same class, e.g.:

foo(new Integer(4),new Integer(5));

Should return true, however:

foo(new SomeClass(), new SubtypeSomeClass());

should return false and also:

foo(new Integer(3),"zoo");

should return false.

I believe one way is to compare the fully qualified class names:

public boolean foo(Object o1, Object o2){
 Class<? extends Object> c1 = o1.getClass();
 Class<? extends Object> c2 = o2.getClass();
 if(c1.getName().equals(c2.getName()){ return true;}
 return false;  
}

An alternative conditional statement would be :

if (c1.isAssignableFrom(c2) && c2.isAssignableFrom(c1)){ return true; }

The latter alternative is rather slow. Are there other alternatives to this problem?

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

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

发布评论

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

评论(2

季末如歌 2024-09-05 02:31:47

不要理会完全限定的类名 - 只需比较类引用:

public boolean foo(Object o1, Object o2) {
    return o1.getClass() == o2.getClass();
}

类在名称和类加载器方面本质上是唯一的 - 因此如果对象属于同一类名称但由不同的类加载,则这将返回 false类加载器,但这可能是合适的:除了名称之外,它们可能是完全不同的类!但是,如果类具有相同的名称和类加载器,它们将具有相同的引用。

请注意,如果 o1o2 为 null,这将抛出 NullPointerException,但这可能正是您想要的。

Don't bother with the fully qualified class names - just compare class references:

public boolean foo(Object o1, Object o2) {
    return o1.getClass() == o2.getClass();
}

Classes are essentially unique by name and classloader - so this will return false if the objects are of the same class name but loaded by different classloaders, but that's probably appropriate: they could be completely different classes in all but name! If the classes have the same name and classloader, however, they'll have the same reference.

Note that this will throw a NullPointerException if either o1 or o2 is null, but again that's probably what you want.

不甘平庸 2024-09-05 02:31:47

你的资格可以相当慢吗?与什么相比?另外请详细说明一下您的使用场景?在 java 中,Object api 包含 equals/hashCode 方法配对,用于识别对象之间的相等性。看起来这就是您可能试图找到的内容,即:

new Integer(4).equals(new Integer(5))

在这种情况下,在 SomeClass 中重写 equals 的最佳实现如下:

public boolean equals(Object obj) {
    if (obj instanceof SomeClass) {
        return ((SomeClass) obj).identityField.equals(identityField);
    }
    return false;
}
public int hashCode() {
    return 31 * identity.hashCode();
}

确保您在对象中提供标识。

Can you qualify rather slow? Compared to what? Also please elaborate your usage scenario? In java the Object api includes a equals/hashCode method paring that should be used to identify equality between objects. It appears as if this is what you may be attempting to find, ie:

new Integer(4).equals(new Integer(5))

In this case, the best implementation for overriding equals in SomeClass is as follows:

public boolean equals(Object obj) {
    if (obj instanceof SomeClass) {
        return ((SomeClass) obj).identityField.equals(identityField);
    }
    return false;
}
public int hashCode() {
    return 31 * identity.hashCode();
}

Making sure you provide an identity within your object.

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