static void Main(string[] args) { MyDate m1 = new MyDate(24, 3, 2001); MyDate m2 = new MyDate(24, 3, 2001); Console.WriteLine(m1 == m2); //不相等,显示false Console.WriteLine(m1.Equals(m2)); //不相等,显示false m1 = new MyOkDate(24, 3, 2001); m2 = new MyOkDate(24, 3, 2001); Console.WriteLine(m1.Equals(m2)); //相等,显示true }
public override bool Equals(object obj) { if (obj is MyOkDate) { MyOkDate m = (MyOkDate)obj; if (m.day == day && m.month == month && m.year == year) { return(true); } } return(false); }