【发布时间】:2011-09-05 10:52:36
【问题描述】:
在下面的代码中,我定义了一个接口,一个带有打印“foo”的方法的抽象基类,一个实现这两者的类,以及接口上的一个扩展方法,其签名等于抽象基类中的方法打印“酒吧”。当我运行这个示例时,为什么打印“bar”而不是“foo”?如果适用,这种语言设计选择背后的士气是什么?
public interface ISomething
{}
public abstract class SomethingElse
{
public void foo()
{
Console.WriteLine("foo");
}
}
public class DefinitelySomething : SomethingElse, ISomething
{}
public static class ISomethingExtensions
{
public static void foo(this ISomething graphic)
{
Console.WriteLine("bar");
}
}
class Program
{
static void Main(string[] args)
{
ISomething g = new DefinitelySomething();
g.foo();
}
}
【问题讨论】:
标签: c# .net extension-methods