为什么不C#允许静态方法来实现接口? [英] Why Doesn't C# Allow Static Methods to Implement an Interface?

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

问题描述

为什么C#设计的这种方式?

据我了解,接口只描述的行为,并提供描述实现某些行为实现的接口类合同义务的目的。

如果类希望实现在一个共享的方法水煤浆,你们为什么不?

下面是什么,我心里有一个例子:

  //这些项目将被显示在屏幕上的列表。
公共接口IListItem {
  串ScreenName链接();
  ...
}公共类动物:IListItem {
    //所有的动物将被称为动物。
    公共静态字符串ScreenName链接(){
        返回动物;
    }
....
}公共类人:IListItem {    私人字符串名称;    //所有的人将其个人的名字被调用。
    公共字符串ScreenName链接(){
        返回名称;
    }    .... }


解决方案

假设你会问,为什么你不能做到这一点:

 公共接口的IFoo {
    酒吧无效();
}公共类Foo:IFoo的{
    公共静态无效的酒吧(){}
}

这是没有道理给我,语义。接口上指定的方法应该在那里,以指定与对象交互的合同。静态方法不允许你与对象进行交互 - 如果您在您的实现可以作出静态的,则可能需要问自己,如果该方法在界面真正属于的位置发现自己。



为了实现你的榜样,我想给动物一个const属性,这将仍然允许它从静态上下文进行访问,并在实施返回值。

 公共类动物:IListItem {
    / *可能很难拿出一个不同的,但有意义的名字!
     *不同的大小写约定,如Java了,将有助于在这里。
     * /
    公共常量字符串AnimalScreenName =动物;
    公共字符串ScreenName链接(){返回AnimalScreenName; }
}

对于更复杂的情况,你总是可以声明另一个静态方法和委托了这一点。在设法拿出一个例子,我想不出任何理由,你会做一些不平凡的两个静态和实例的上下文,所以我就饶你一FooBar的斑点,并把它作为一个迹象表明,它可能不是一个好主意。

Why was C# designed this way?

As I understand it, an interface only describes behaviour, and serves the purpose of describing a contractual obligation for classes implementing the interface that certain behaviour is implemented.

If classes wish to implement that behavour in a shared method, why shouldn't they?

Here is an example of what I have in mind:

// These items will be displayed in a list on the screen.
public interface IListItem {
  string ScreenName();
  ...
}

public class Animal: IListItem {
    // All animals will be called "Animal".
    public static string ScreenName() {
        return "Animal";
    }
....
}

public class Person: IListItem {

    private string name;

    // All persons will be called by their individual names.
    public string ScreenName() {
        return name;
    }

    ....

 }

解决方案

Assuming you are asking why you can't do this:

public interface IFoo {
    void Bar();
}

public class Foo: IFoo {
    public static void Bar() {}
}

This doesn't make sense to me, semantically. Methods specified on an interface should be there to specify the contract for interacting with an object. Static methods do not allow you to interact with an object - if you find yourself in the position where your implementation could be made static, you may need to ask yourself if that method really belongs in the interface.


To implement your example, I would give Animal a const property, which would still allow it to be accessed from a static context, and return that value in the implementation.

public class Animal: IListItem {
    /* Can be tough to come up with a different, yet meaningful name!
     * A different casing convention, like Java has, would help here.
     */
    public const string AnimalScreenName = "Animal";
    public string ScreenName(){ return AnimalScreenName; }
}

For a more complicated situation, you could always declare another static method and delegate to that. In trying come up with an example, I couldn't think of any reason you would do something non-trivial in both a static and instance context, so I'll spare you a FooBar blob, and take it as an indication that it might not be a good idea.

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

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