简体   繁体   English

C#从多个抽象类派生一个类

[英]C# Deriving a class from more than one abstract class

I created two abstract classes and tried to create a class that inherits from both. 我创建了两个抽象类,并试图创建一个从这两个类继承的类。 But I get an error message. 但是我收到一条错误消息。

abstract class AbstractClassOne
{
    public abstract void ShowMessage();
    public abstract void DisplayName();
}

abstract class AbstractClassTwo
{
    public abstract void ShowMessage();
    public abstract void DisplayPlace();
}

class DerivedClass : AbstractClassOne, AbstractClassTwo // here under AbstractClassTwo it shows the error "cannot have multiple base classes:"
{

}

So a class can only derive from one abstract class? 因此,一个类只能从一个抽象类派生吗?

If can derive from more than one abstract class, then what happens if both classes define the same method, as is the case above (abstract class one and two both have a method showmessage() , so which one will be in the derived class)? 如果可以从一个以上的抽象类派生,那么如果两个类都定义相同的方法(如上例),将会发生什么(抽象类一和两个都具有方法showmessage() ,因此哪个类将在派生类中) ?

In C# it's not allowed to inherit from more than one class. 在C#中,不允许从多个类中继承。 To do what you want here, you need to use interfaces. 要在这里执行您想要的操作,您需要使用接口。

abstract class AbstractClassOne
{
    public abstract void ShowMessage();
    public abstract void DisplayName();
}

Interface IClassTwo
{
    void ShowMessage();
    void DisplayPlace();
}

class DerivedClass : AbstractClassOne, IClassTwo
{

}

Multiple inheritance is not allowed by C# but it is allowed by C++. C#不允许多重继承,但C ++允许多重继承。 To answer your question regarding the ShowMessage() method that is a known problem in c++ with multiple inheritance called "The Diamond Problem". 回答有关ShowMessage()方法的问题,这是c ++中具有多个继承的一个已知问题,称为“钻石问题”。 see: http://en.wikipedia.org/wiki/Multiple_inheritance 参见: http : //en.wikipedia.org/wiki/Multiple_inheritance

So basically you will have to excitability state to which method you are refereeing when calling it eg ParentA::ShowMessage() 因此,基本上,您将必须在ParentA::ShowMessage()状态下声明调用该方法时要引用的方法,例如ParentA::ShowMessage()

if you want to have a type that is polymorphic to 2 other types than you should create two separate interfaces and implement them. 如果您要具有与其他2种类型相同的类型,则应创建两个单独的接口并实现它们。 and if you want to reuse the same methods than you will have to use compositions. 如果您想重用与使用合成相同的方法,则必须使用合成。

Interfaces example: 接口示例:

public interface ISomeInterface
{
    public void ShowMessage();
    public void DisplayName();
}
public class ClassOne : ISomeInterface
{
    public void ShowMessage()
    {
       //implementation
    }

    public void DisplayName()
    {
       //implementation
    }
}

public class ClassTwo : ISomeInterface
{
    public void ShowMessage()
    {
       //implementation
    }

    public void DisplayPlace()
    {
       //implementation
    }
}

Interface with reusable Show Message Method using composition: 使用以下内容与可重用的Show Message Method接口:

public class ClassTwo : ISomeInterface
{
    private ISomeInterface _MyPrivateReusableComponent = new ClassOne();

    public void ShowMessage()
    {
       _MyPrivateReusableComponent.ShowMessage()
    }

    public void DisplayPlace()
    {
         _MyPrivateReusableComponent.DisplayName()
        //implementation
    }
}

您不能从多个类(抽象类或其他类)继承,但是在您的情况下,抽象类是很多接口,因此您可以将它们转换为接口并从它们继承(可以从任意数量的接口继承)。

No, abstract class whether having all abstract methods or only some, makes no difference as far as inheritance in concerned. 不,抽象类是否具有全部抽象方法或仅具有某些抽象方法,就所涉及的继承而言没有任何区别。 you can inherit only one class (in C#) and as many interfaces as you want. 您只能继承一个类(在C#中),并可以继承任意数量的接口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM