using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
class Program
{
static void Main(string[] args)
{
//以下代码在没有重写Equals的情况下
//Codon p1 = new Codon("dd", 23);
//Codon p2 = new Codon("dd", 23);
//if (p1.Equals(p2))
//{
// Console.Write("Equal");
//}
//else
//{
// Console.Write("Not Equal");//输出Not Equal
//}
//重写Equals后,结果如下:
Codon p1 = new Codon(“dd", 23);
Codon p2 = new Codon("dd", 23);
if (p1.Equals(p2))
{
Console.Write("Equal");//输出Equal
}
else
{
Console.Write("Not Equal");
}
Console.Read();
}
}
class Codon
{
public string Name { get; set; }
public int Age { get; set; }
public Codon()
{
}
public Codon(string name,int age)
{
this.Name = name;
this.Age = age;
}
public override bool Equals(object obj)
{
Codon codon=obj as Codon;
return this.Name == codon.Name && this.Age == codon.Age;
}
}
}