このクラスで equals() メソッドをオーバーライドしたい。equals() メソッドをオーバーライドしている間は通常のルールに従いますが、オブジェクトをクラス型に型キャストします
しかし、私の equals() メソッドでは、オブジェクトが同じジェネリック型である場合にのみ true を返したいと考えています。
equals() メソッドでインスタンスのランタイム タイプを確認するにはどうすればよいですか?
これが私のコードです:
public class GenericsRunTimeType<T> {
private T foo;
public GenericsRunTimeType(T foo){
this.foo = foo;
}
@Override
public boolean equals(Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
// Before doing this I want to test if obj and this are of the same generic type
GenericsRunTimeType other = (GenericsRunTimeType) obj;
if(other.equals(obj))
return true;
else
return false;
}
}