简体   繁体   English

tclass扩展了一个抽象类,并使用相同的签名方法实现了接口

[英]tclass extends an abstract class and implements interface with same signature method

I am confused with this scenario of an abstract class and interface having same signature method. 我对具有相同签名方法的抽象类和接口的这种情况感到困惑。 How many definitions will be there in deriving class? 派生类中会有多少个定义? How will the call be resolved? 通话将如何解决?

public abstract class AbClass
{
    public abstract void printMyName();
}

internal interface Iinterface
{
    void printMyName();
}

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
}

There will be just one implementation in default scenario, but you can have two implementation if you'll define method with void Iinterface.printMyName signature. 在默认情况下,只有一个实现,但是如果要定义带有void Iinterface.printMyName签名的方法,则可以有两个实现。 Take a look at SO question about Difference between Implicit and Explicit implementations . 看一下关于隐式和显式实现之间的区别的 SO问题。 Also you have some errors in your sample 您的样本中也有一些错误

  • printMyName in AbClass is not marked as abstract, therefore it should have body. printMyName中的printMyName未标记为抽象,因此应具有主体。
  • if you want to have abstract method - it can't be private 如果要使用抽象方法 -不能为私有方法

- --

public abstract class AbClass
{
    public abstract void printMyName();
}

internal interface Iinterface
{
    void printMyName();
}

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    public override void printMyName()
    {
        Console.WriteLine("Abstract class implementation");
    }

    //You can implement interface method using next signature
    void Iinterface.printMyName()
    {
        Console.WriteLine("Interface implementation");
    }
}

public class MainClass_WithoutExplicityImplementation : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    public override void printMyName()
    {
        Console.WriteLine("Abstract class and interface implementation");
    }
}

Example of usage 使用例

var mainInstance = new MainClass();
mainInstance.printMyName();      //Abstract class implementation
Iinterface viaInterface = mainInstance;
viaInterface.printMyName();      //Interface implementation


var mainInstance2 = new MainClass_WithoutExplicityImplementation();
mainInstance2.printMyName();      //Abstract class and interface implementation
Iinterface viaInterface = mainInstance2;
viaInterface.printMyName();      //Abstract class and interface implementation

You can ommit the implementation of the interface within your concrete class, as the base class already implements it. 您可以省略具体类中接口的实现,因为基类已经实现了它。 However you might also implement the interface explicitly meaning you may "override" the behaviour from your base (the-abstract) class (overriding is not real the correct word here). 但是,您也可以显式实现该接口,这意味着您可以“覆盖”基类(抽象类)的行为(在这里,“覆盖”不是真正的正确单词)。 This further expects to cast your instance explicitky to the interface to call that method: 这进一步期望将您的实例显式转换为接口以调用该方法:

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    void Iinterface.printMyName()
    {
        throw new NotImplementedException();
    }
}

You may call this cia ((Iinterface(myMainClassInstance).printMyName() . If you call myMainClassInstance.printMyName however the base-implementation is called. 您可以将其称为cia ((Iinterface(myMainClassInstance).printMyName() 。如果调用myMainClassInstance.printMyName则会调用基本实现。

If you want to support a base-implementatation within your base-class you may however make the method virtual and override it within your derived class. 如果要在基类中支持基本实现,则可以将方法设为virtual方法,并在派生类中重写该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类都扩展了抽象类并实现了接口 - Class both extends an abstract class and implements an interface 接口方法的签名包括一个class,它实现了接口本身 - Signature of interface's method includes a class, which implements the interface itself 抽象 class 和接口与相同的泛型方法 - Abstract class and interface with the same generic method 如何模拟扩展类并实现接口的对象? - How to mock an object that extends a class and implements an interface? C# 实现接口并扩展通用 class - C# implements interface and extends generic class 强制转换实现接口的通用抽象类 - Casting in generic abstract class that implements interface 对实现通用接口的抽象类执行“保护的抽象替代” - Perform a 'protected abstract override' for an abstract class which implements a generic interface 当嘲笑实现具有相同方法签名的多个接口的接口时,Moq Setup InvalidCastException - Moq Setup InvalidCastException when Mocking an interface that implements multiple interfaces having the same method signature 使用抽象类和接口的原因? (抽象class实现接口) - Reason to use BOTH abstract classes and interfaces? (Abstract class implements interface) 接口方法返回类型是实现接口的类 - Interface method return type to be class that implements the interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM