开发者

Avoid warning when implementing equals on class with generics

I have the following code:

public abstract class A<T extends B<? extends A<T>>>{

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
    开发者_StackOverflow        return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        A other = (A) obj; // warning here: "A is a raw type"
        // [...]
    }
}

How to avoid both "A is a raw type" and "Type safety: unchecked cast" at the specified line? Is there a hack of some sort or I am doing something wrong with my classes?

Thanks


If the parameterized type of the compared A doesn't matter, declare and cast it as A<?>:

A<?> other = (A<?>) obj;

This will remove the warning.


Put this annotation before it: @SuppressWarnings("unchecked"). See here for more info.


You can use the @SuppressWarnings annotation as needed. See here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜