C# 如何通过保留方法名称来扩展接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8963542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:22:54  来源:igfitidea点击:

How to extend an interface by keeping the method names?

c#interface

提问by Omar

Given two interfaces:

给定两个接口:

interface I1 {
    int Foo();
}

interface I2 {
    void Foo();
}

And a class:

还有一个类:

class Test : I1, I2 {
    int I1.Foo() {
        Console.WriteLine("I1.Foo");
        return default(int);
    }

    public void Foo() {
        Console.WriteLine("I2.Foo");
    }
}

How can I extend the interface I2with I1by keeping the methods named Foo?

我怎么能扩展接口I2I1通过保持命名的方法Foo

I tried the following code but it doesn't compile:

我尝试了以下代码,但无法编译:

interface I1 {
    int Foo();
}

interface I2 : I1 {
    void I2.Foo();
} 

class Test : I2 { /* same code */ }

采纳答案by drf

It is unclear in the example what explicitly declaring I2.Foo()in the interface itself would accomplish if it were permitted. The specification (s. 13.4.1) allows the struct or class implementing an interface to declare an explicit member implementation. (Interfaces cannot declare any implementation, explicit or otherwise).

在示例中不清楚I2.Foo()如果允许的话,在接口本身中明确声明会完成什么。规范(第 13.4.1 节)允许实现接口的结构或类声明显式成员实现。(接口不能声明任何实现,显式或其他)。

Thus, suppose we have defined:

因此,假设我们已经定义:

interface IFoo
{
    void Bar();
}

interface IBaz : IFoo
{
    new void Bar();
}

interface IQux : IBaz { }

class A : IQux // equivalent to class A : IQux, IBaz, IFoo (spec sec. 13.4.6)
{
    void IFoo.Bar()
    {
        Console.WriteLine("IFoo.Bar");
    }

    void IBaz.Bar()
    {
        Console.WriteLine("IBaz.Bar");
    }

    public void Bar()
    {
        Console.WriteLine("A.Bar");
    }

    // Not allowed: void IQux.Bar() {...}
    // Since "The fully-qualified name of the interface member
    // must reference the interface in which the member
    // was declared" (s. 13.4.1)
}

Then the following driver shows the effect of the explicit interface method implementation.

那么下面的驱动就展示了显式接口方法实现的效果。

public static void Main()
{
    A a = new A();
    a.Bar(); // prints A.Bar
    (a as IFoo).Bar(); // prints IFoo.Bar
    (a as IBaz).Bar(); // prints IBaz.Bar
    (a as IQux).Bar(); // prints IBaz.Bar
}

回答by Keith Nicholas

not quite sure what you are wanting it to do, but you can do :-

不太确定你想要它做什么,但你可以这样做:-

public interface I1
{
    int Foo();
}

public interface I2:I1
{
    new void Foo();
}

回答by Olivier Jacot-Descombes

It works only if the two methods have a different signature, which means that they must have a different number of parameters or different types of parameters or both.

只有当这两个方法具有不同的签名时才有效,这意味着它们必须具有不同数量的参数或不同类型的参数或两者兼而有之。

Why not name your two methods GetFooand DoFoo?

为什么不命名你的两个方法GetFooDoFoo

This would work

这会工作

public interface I1 
{ 
    int Foo(); 
} 

public interface I2 : I1 
{ 
    void Foo(int i); 
} 

This would also work

这也行

public interface I1 
{ 
    int GetFoo(); 
} 

public interface I2 : I1 
{ 
    void DoFoo(); 
} 

You could also declare a property. Properties consist of two methods: A getter and a setter.

你也可以声明一个属性。属性由两种方法组成:getter 和 setter。

public interface I
{
    int Foo { get; set; }
}

public class C : I
{
    private int _foo;
    public int Foo
    {
        get {
            // getter
            return _foo;
        }
        set {
            // setter
            _foo = value;
        }
    }
}