静态方法无法实现接口方法,为什么呢? [英] static method cannot implement interface method, why?

查看:345
本文介绍了静态方法无法实现接口方法,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface IXXX
{
    void Foo();
}

class XXX : IXXX
{
    public static void Foo()
    {
        Console.WriteLine("From XXX");
    }
}


class Program 
{
    static void Main(string[] args)
    {
        XXX.Foo();

    }
}



编译器错误:XXX.Foo(因为它是静态的)无法实现接口成员。

Compiler error: XXX.Foo() cannot implement an interface member because it is static.

为什么不能静态方法实现接口的方法是什么?

Why can't a static method implement an interface method?

推荐答案

请参阅此线程从 JoelOnSoftware 描述这背后的原因。

See this thread from JoelOnSoftware describing the reasons behind this.

基本上接口是消费者与供应商之间的合同,以及一个静态方法属于类,而不是类作为这样的每个实例

Basically the interface is the contract between the consumer and the provider, and a static method belongs to the class, and not each instance of the class as such.

上也是如此处理完全一样的问题,前面一个问题:
为什么不C#允许静态方法来实现接口?

An earlier question on SO also deal with the exact same question: Why Doesn't C# Allow Static Methods to Implement an Interface?

这篇关于静态方法无法实现接口方法,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆