我想重写泛型类中的 equals() 方法,为此,我必须将 Object 转换为我的泛型类型 Pair。
我添加了@SuppressWarnings(“未选中”)来“静音”警告,但问题仍然存在。方法也不适用于泛型类型,因此使用 T.getType() 是不可能的。
public class Pair<T, U> {
private T first;
private U second;
public Pair(T _first, U _second)
{
first = _first;
second = _second;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
//Problem ahead!!!
Pair<T, U> other = (Pair<T, U>) obj;
...
}
}
是否有任何普遍的良好做法或“技巧”可以正确,安全地做到这一点?
慕容森
交互式爱情
喵喔喔
慕的地6264312
互换的青春
相关分类