如何实现“等于”使用“instanceof”的泛型方法? [英] How to implement "equals" method for generics using "instanceof"?

查看:1793
本文介绍了如何实现“等于”使用“instanceof”的泛型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受泛型的类,我想以一种非尴尬的方式覆盖 equals 方法看起来很干净并且代码量很小,但是对于一个非常普遍的用例)。

现在我有这样的东西:

  public class SingularNode< T> {
私人T值;

@SuppressWarnings(unchecked)
@Override
public boolean equals(Object other){
if(其他instanceof SingularNode<>){$ b (((SingularNode< T>)other).value.equals(value)){
返回true;
}
}
返回false;


$ / code $ / pre

其中,我猜测,这是相当有缺陷的 - 我正在其他对象上转换为 SingularNode< T> ,这可能会导致误差的。



另一件事是 - 当我做了 if(其他instanceof SingularNode<>)我其实不是检查完全正确的事情。我实际上想检查类型 T ,而不是输入。每当我尝试将变成 T 时,我会遇到如下错误:


无法对参数化类型 SingularNode< T> 执行instanceof检查。使用表单 SingularNode<?> 代替,因为进一步的泛型类型信息将在运行时被擦除

我该如何解决这个问题?有没有办法做 T.class.isInstance(other);



我想有一个非常丑陋hack解决方案:

  @SuppressWarnings(unchecked)
public boolean isEqualTo(Class<?> c ,Object obj){
if(c.isInstance(obj)& c.isInstance(this)){
if(((SingularNode< T>)obj).value.equals(value )){
return true;
}
}
返回false;
}

但是这只是看起来非常尴尬,额外的方法参数,它也不是像等于的内置函数是。



任何懂得泛型的人请解释一下。我不是那种精通Java的人,因为你可以清楚地看到,所以请详细解释一下!

解决方案

此版本不提供任何警告。

  public boolean equals(Object other){
if(其他instanceof SingularNode<?> ){
if(((SingularNode<> other).value.equals(value)){
return true;
}
}
返回false;
}

至于转换为 SingularNode< T> code>它没有任何帮助,你不能假定 T 可以是 Object 之外的任何东西。



了解更多关于如何在Java中编译泛型的信息
$ b


https://docs.oracle.com/javase/tutorial/java/generics/erasure。 html



I have a class that accepts a generic type, and I want to override the equals method in a non-awkward way (i.e. something that looks clean and has minimal amount of code, but for a very general use case).

Right now I have something like this:

public class SingularNode<T> {
    private T value;

    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object other){
        if(other instanceof SingularNode<?>){
            if(((SingularNode<T>)other).value.equals(value)){
                return true;
            }
        }
        return false;
    }
}

Which, I'm guessing, is pretty flawed - I'm doing a cast to SingularNode<T> on the other object, which could potentially throw an error.

Another thing is - when I do if(other instanceof SingularNode<?>) I'm actually not checking exactly the right thing. I actually want to check against type T and not type ?. Whenever I try to make the ? into T, I get some error like:

Cannot perform instanceof check against parameterized type SingularNode<T>. Use the form SingularNode<?> instead, since further generic type information will be erased at runtime

How can I get around this? Is there some way to do T.class.isInstance(other); ?

I suppose there's one really ugly hack solution like this:

@SuppressWarnings("unchecked")
public boolean isEqualTo(Class<?> c, Object obj){
    if(c.isInstance(obj) && c.isInstance(this)){
        if(((SingularNode<T>)obj).value.equals(value)){
            return true;
        }
    }
    return false;
}

But that just looks really awkward with the extra method parameter, and it's also not a built-in function like equals is.

Any one who understand generics please explain this? I'm not that proficient with Java, as you can clearly see, so please explain with a tad bit more detail!

解决方案

This version gives no warnings

public boolean equals(Object other){
    if (other instanceof SingularNode<?>){
        if ( ((SingularNode<?>)other).value.equals(value) ){
            return true;
        }
    }
    return false;
}

As for casting to SingularNode<T> it does not help anything, you cannot assume that T can be anything but Object.

Learn more about how generics are compiled in Java at

https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

这篇关于如何实现“等于”使用“instanceof”的泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆