C# 用抽象方法显式实现接口

C# 用抽象方法显式实现接口,c#,interface,abstract,C#,Interface,Abstract,这是我的界面: public interface MyInterface { bool Foo(); } 这是我的抽象类: public abstract class MyAbstractClass : MyInterface { abstract bool MyInterface.Foo(); } 这是编译器错误: “修饰符'abstract'对此项无效 我应该如何用抽象方法显式实现抽象呢?我不知道为什么需要。为什么不让抽象类的具体实现从接口实现成员呢?事实上是一样的。an

这是我的界面:

public interface MyInterface {
    bool Foo();
}
这是我的抽象类:

public abstract class MyAbstractClass : MyInterface {
    abstract bool MyInterface.Foo();
}
这是编译器错误: “修饰符'abstract'对此项无效


我应该如何用抽象方法显式实现抽象呢?

我不知道为什么需要。为什么不让抽象类的具体实现从接口实现成员呢?事实上是一样的。

an没有实现,所以不能用它显式实现接口hod.

基本上,你不能。无论如何,不能直接。你不能覆盖一个显式实现接口的方法,你必须覆盖一个抽象方法。最接近的方法是:

bool MyInterface.Foo() {
    return FooImpl();
}

protected abstract bool FooImpl();

仍然显式实现接口并强制派生类实际提供实现。这些是您试图实现的方面吗?

您必须使用接口成员的隐式实现,而不是显式实现:

public abstract class MyAbstractClass : MyInterface
{
    public abstract bool Foo();
}
我能做到这一点

public interface SampleInterface
{
    void member1();
    void member2();
    void member3();
}

public abstract class Client2 : SampleInterface.SampleInterface
{
    public void member1()
    {
        throw new NotImplementedException();
    }

    public abstract void member2();


    public void member3()
    {
        throw new NotImplementedException();
    }
}

public class Client3 : Client2
{
    public Client3()
    {

    }

    public override void member2()
    {
        throw new NotImplementedException();
    }
}

除了Jon的解释:这是一种绕过问题而不是直接解决问题的方法,只在某些情况下有效,但也许有人会从这个想法中受益

如果计划将所有(或至少大多数)接口方法调用传递给派生类,则可以通过以下方式执行:

public interface MyInterface 
{
    bool Foo();
}

public abstract class MyAbstractClass
{
    public abstract MyInterface AsMyInterface();
}

public class MyDerivedClass : MyInterface
{
    public override MyInterface AsMyInterface()
    {
        return this;
    }

    public bool Foo()
    {
        return false;
    }
}

...

MyAbstractClass c = new MyDerivedClass();
MyInterface i = c.AsMyInterface();
bool b = i.Foo();

抽象在抽象类中实现的所有接口方法,即使您不使用它们

这种特殊情况要求您使用接口实现2个或更多抽象类的层次结构

我还试图在C#中实现一个层次结构。我需要一个接口,但我想要一个抽象类,因为接口的大多数属性都是相同的。为此,我必须创建一个单独的抽象类,带有实现,然后我的具体类,或者在我的情况下另一个抽象类,将继承接口和抽象类

出于许多原因,我不认为第一个是一个好例子,但我必须拥有它,因为编译器不允许FooBar实现Foo,然后有另一个抽象类继承FooBar。因此,我有一个抽象类,带有抽象方法bar(),接口带有bar()方法

这是我第一次尝试,然后我变得好奇并开始思考。我发现了这个解决方案。更好的解决方案

public interface Foo { 
    bool bar();
    bool buzz();
    //other stuffs
}


public abstract class FooBar : Foo{
    public abstract bool bar();
    public abstract bool buzz();
    //Other stuffs
}

public abstract class FooBarAbstraction: FooBar {
     //other stuffs
     //Don't supply the interface and abstract here
     // override everything else
     public override bool buzz() {
         return false;
     }
}

public class FooBarConcrete: FooBarAbstraction {
    public override bool bar() {
        return true;
    }
   //other stuffs
}

事实上,还有另一种选择,而不是使用抽象助手方法,该方法仍然保持实现的私有性:

public abstract class MyAbstractClass : MyInterface
{
  bool MyInterface.Foo() // must be overridden
  { throw NotImplementedException(); // never called
  }
}

public class MyDerivedClass : MyAbstractClass, MyInterface
{
  bool MyInterface.Foo() // overrides MyInterface.Foo
  { // Place your implementation here
  }
}
如果接口有许多方法,并且在派生类中只重新定义了其中的一些方法,那么这种模式也可以工作。当然,您也可以使用这种模式来覆盖私有接口实现

主要的缺点是,Foo不能在MyAbstractClass中声明为抽象的,因此编译器无法确保该方法实际上被重写。(遗憾的是,抽象类在C#中可能没有不完整的接口实现。)


优点是您保存了一条可能导致CPU管道暂停的CALI指令。但是,影响很小,因为由于接口调用,该方法无论如何都无法内联。因此,我建议仅在性能关键的情况下使用它。

是。我想强制从MyAbstractClass继承的类实现Foo(),但同时我希望MyAbstractClass显式实现MyInterface.Foo()。这就是我在代码中所做的。虽然这是——正如我所相信的——唯一直接的解决方案,但它有一个缺点,那就是需要声明相同的方法三次-显式接口实现,然后是受保护的抽象方法,最后是在派生类中实现。我很好奇,为什么不能直接实现-既然MyAbstractClass是抽象类,接口所需的方法可能会被视为该类的抽象方法,确保它们在某些派生类中实现公共接口MyInterface{bool Foo();}公共抽象类MyAbstractClass:MyInterface{}公共类ConcreteClass:MyAbstractClass{}问题是MyAbstractClass应该从MyInterface()实现Foo()。我不确定你说的“MyAbstractClass应该从MyInterface()实现Foo()是什么意思“。在您的示例中,您希望Foo()是抽象的,因此您没有实现它。如果ConcreteClass不提供Foo()的实现,它将不会编译。感谢您指出显而易见的,同时也很明显地指出我对某些C#语法的无知。我明白了。这是一个非常直截了当的问题。太遗憾了,我没有更多的投票权。您正在隐式而不是显式地实现此接口。请阅读差异:
public abstract class MyAbstractClass : MyInterface
{
  bool MyInterface.Foo() // must be overridden
  { throw NotImplementedException(); // never called
  }
}

public class MyDerivedClass : MyAbstractClass, MyInterface
{
  bool MyInterface.Foo() // overrides MyInterface.Foo
  { // Place your implementation here
  }
}