1.Equals & IsNullOrEmpty
要求—-想要对Point类写一个类似string.IsNullOfEmpty的函数检查经纬度是否为0即Empty
public class Point
{
public double Lat{get;set;}
public double Lon{get;set;}
public string Tag{get;set;}
public Person(double lon, double lat)
{
this.Lat = lat;
this.Lon = lon;
}
public Point(double lon,double lat,string tag)
{
this.Lon = lon;
this.Lat = lat;
this.Tag = tag;
}
}
想到了重写Equals的步骤—-在VS中写Equals并Tab两次则会出现完整的Equals函数
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if ((obj.GetType().Equals(this.GetType())) == false)
{
return false;
}
Point point = null;
point = (Point)obj;
return this.Lat.Equals(point.Lat) && this.Lon.Equals(point.Lon);
}
// override object.GetHashCode
public override int GetHashCode()
{
return this.Lat.GetHashCode() + this.Lon.GetHashCode();
}
然后再写IsNullOrEmpty函数
public static bool IsNullOrEmpty(Point p)
{
if (p == null)
{
return true;
}
else if (p.Equals(new Point(0, 0)))
{
return true;
}
else
{
return false;
}
}