方法重载和继承
Method overloading and inheritance
我有以下 类:
public class BaseRepository
{
public virtual void Delete(int id)
{
Console.WriteLine("Delete by id in BaseRepository");
}
}
public class EFRepository: BaseRepository
{
public override void Delete(int id)
{
Console.WriteLine("Delete by Id in EFRepository");
}
public void Delete(object entity)
{
Console.WriteLine("Delete by entity in EFRepository");
}
}
然后我这样使用它:
var repository = new EFRepository();
int id = 1;
repository.Delete(id);
为什么在那种情况下只有 EFRepository.Delete(object entity)
会打电话?
基本上,方法调用在 C# 中的工作方式是编译器首先查看最派生的 class,然后查看是否有任何 新声明的 方法(不包括覆盖)适用于调用的参数。如果至少有一种适用的方法,重载解析会找出最好的方法。如果没有,它会尝试基 class,依此类推。
我同意这很令人惊讶 - 这是对 "brittle base class" 问题的一次尝试,但我个人更希望任何 overridden 方法都包含在候选集中。
C# 5 规范的第 7.6.5.1 节中描述了方法调用。这里的相关部分是:
- The set of candidate methods is reduced to contain only methods from the most derived types: For each method
C.F
in the set, where C
is the type in which the method F
is declared, all methods declared in a base type of C
are removed from the set. Furthermore, if C
is a class type other than object, all methods declared in an interface type are removed from the set. (This latter rule only has affect when the method group was the result of a member lookup on a type parameter having an effective base class other than object and a non-empty effective interface set.)
并且在 7.4 的成员查找部分,override
方法被显式删除:
Members that include an override
modifier are excluded from the set.
因为:public override void Delete(int id) 将仅覆盖基础class方法其中一个:public virtual void删除(int id).
虽然 public void Delete(object entity) 是 EFRepository class 方法,所以当您从 EFRepository 对象执行方法 它将 调用自己的方法 这只不过是 public void Delete(object entity)
我有以下 类:
public class BaseRepository
{
public virtual void Delete(int id)
{
Console.WriteLine("Delete by id in BaseRepository");
}
}
public class EFRepository: BaseRepository
{
public override void Delete(int id)
{
Console.WriteLine("Delete by Id in EFRepository");
}
public void Delete(object entity)
{
Console.WriteLine("Delete by entity in EFRepository");
}
}
然后我这样使用它:
var repository = new EFRepository();
int id = 1;
repository.Delete(id);
为什么在那种情况下只有 EFRepository.Delete(object entity)
会打电话?
基本上,方法调用在 C# 中的工作方式是编译器首先查看最派生的 class,然后查看是否有任何 新声明的 方法(不包括覆盖)适用于调用的参数。如果至少有一种适用的方法,重载解析会找出最好的方法。如果没有,它会尝试基 class,依此类推。
我同意这很令人惊讶 - 这是对 "brittle base class" 问题的一次尝试,但我个人更希望任何 overridden 方法都包含在候选集中。
C# 5 规范的第 7.6.5.1 节中描述了方法调用。这里的相关部分是:
- The set of candidate methods is reduced to contain only methods from the most derived types: For each method
C.F
in the set, whereC
is the type in which the methodF
is declared, all methods declared in a base type ofC
are removed from the set. Furthermore, ifC
is a class type other than object, all methods declared in an interface type are removed from the set. (This latter rule only has affect when the method group was the result of a member lookup on a type parameter having an effective base class other than object and a non-empty effective interface set.)
并且在 7.4 的成员查找部分,override
方法被显式删除:
Members that include an
override
modifier are excluded from the set.
因为:public override void Delete(int id) 将仅覆盖基础class方法其中一个:public virtual void删除(int id).
虽然 public void Delete(object entity) 是 EFRepository class 方法,所以当您从 EFRepository 对象执行方法 它将 调用自己的方法 这只不过是 public void Delete(object entity)