我有一个名为Employee
. 现在我尝试定义它的Equals
方法,但我只想接受一个Employee
as 参数。
所以我写了这段代码:
class MainClass
{
public static void Main (string[] args)
{
Employee e = new Employee();
Employee e2 = new Employee();
Console.WriteLine(Equals(e, e2));
}
public static Employee CreateEmployee()
{
return new Employee();
}
}
class Employee
{
public int ID;
public bool Equals (Employee e)
{
Console.WriteLine("Compare!");
return ID == e.ID;
}
}
但它不起作用!控制台输出:
false
不是我所期望的:
Compare!
true
看起来我必须写public override bool Equals (Object)
,但为什么呢?C#不能根据参数类型选择调用的方法吗?