我想重载==和!=,代码如下.但是提示有错:"定义运算符 == 或运算符 !=,但不重写 Object.Equals(object o)";"定义运算符 == 或运算符 !=,但不重写 Object.GetHashCode()".要怎么搞?
public static bool operator ==(Point P1, Point P2)
{
if (System.Object.ReferenceEquals(P1 , P2 ))
{
return true;
}
if (((object)P1 == null) || ((object)P2 == null))
{
return false;
}
if (Math.Abs(P1.X - P2.X) < 1 && Math.Abs(P1.Y - P2.Y) < 1 )return true ;
else return false ;
} public static bool operator !=(Point P1, Point P2)
{
return !(P1 == P2 );
}
public static bool operator ==(Point P1, Point P2)
{
if (System.Object.ReferenceEquals(P1 , P2 ))
{
return true;
}
if (((object)P1 == null) || ((object)P2 == null))
{
return false;
}
if (Math.Abs(P1.X - P2.X) < 1 && Math.Abs(P1.Y - P2.Y) < 1 )return true ;
else return false ;
} public static bool operator !=(Point P1, Point P2)
{
return !(P1 == P2 );
}
{
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;
}
}
或者要怎么重载equal()和GetHashCode()?