【发布时间】:2013-02-26 21:34:58
【问题描述】:
我遇到了一些代码,但不太确定它为什么会起作用,或者您为什么要这样做。如果有人能为我拆掉它,我会很高兴的。我确实很了解 OOP 概念,我以前从未见过这种技术。谢谢
示例如下:
public interface IInterface
{
IEnumerable<object> DoSomething();
}
public abstract class MyBase : IInterface
{
protected MyBase()
{
}
IEnumerable<object> IInterface.DoSomething()
{
return DoSomething();
}
protected virtual IEnumerable<object> DoSomething()
{
return new List<object>();
}
}
public class MyClass : MyBase
{
internal MyClass() : base() {}
protected override IEnumerable<object> DoSomething()
{
return new List<object>();
}
}
【问题讨论】:
-
不清楚您对示例的哪个方面感到困惑。
-
我猜这是带有受保护的隐式实现的显式接口实现(如果是这样的话 - 我会说隐式接口实现是公共的)。
-
我很抱歉。让我困惑的部分是在我看来是返回虚拟 DoSomething 的 IInterface.DoSomething 的私有实现。我还注意到了 IInterface。它是编译所必需的。
-
这没有意义。看起来有人试图从基类中隐藏
DoSomething。
标签: c# oop class interface protected