看起来您正在用C#进行编码,如果您想使用其他指标而不是“这两个指针(因为对象句柄就是指针)来比较两个对象,则您的类应使用一种称为Equals的方法进行编码。相同的内存地址?”。
我从这里获取了一些示例代码:
class TwoDPoint : System.Object{ public readonly int x, y; public TwoDPoint(int x, int y) //constructor { this.x = x; this.y = y; } public override bool Equals(System.Object obj) { // If parameter is null return false. if (obj == null) { return false; } // If parameter cannot be cast to Point return false. TwoDPoint p = obj as TwoDPoint; if ((System.Object)p == null) { return false; } // Return true if the fields match: return (x == p.x) && (y == p.y); } public bool Equals(TwoDPoint p) { // If parameter is null return false: if ((object)p == null) { return false; } // Return true if the fields match: return (x == p.x) && (y == p.y); } public override int GetHashCode() { return x ^ y; }}Java具有非常相似的机制。该 的equals() 方法是一部分 对象 类和类重载,如果你想这种类型的功能。
对于对象而言,重载’==’可能是个坏主意的原因是,通常,您仍然希望能够进行“这些指针是否相同”比较。这些通常用于例如将元素插入到不允许重复的列表中,并且如果此操作符以非标准方式重载,则某些框架内容可能无法正常工作。



