.net 抽象类的多重继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12694417/
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-09-03 16:38:37  来源:igfitidea点击:

.net Multiple inheritance from abstract classes

.netabstract-classmultiple-inheritance

提问by ???

it is possible to implement this functionality? I've two abstract classes, and i want to force a third class to implement all the abstract methods in the two classes

可以实现这个功能吗?我有两个抽象类,我想强制第三个类实现两个类中的所有抽象方法

public abstract class A
{
    private void MethodA()
    {
        var fool = Prop*2;
    }
    public abstract int Prop { get; }
}
public abstract class B
{
    private void MethodB()
    {
        //.... 
        SomeMethod();
        //....
    }
    public abstract void SomeMethod();
}

public class C: A, B
{
    public int Prop { .... }
    public void SomeMethod { .... }
}

The main problem here is that the implemented final class methods are then used in base abstract classes, and this cannot be achieved with interfaces. So there is no a workaround?

这里的主要问题是实现的最终类方法然后在基础抽象类中使用,而这无法通过接口实现。所以没有解决办法吗?

I've read many similar q/a but nothing responding to my problem. Thanks

我已经阅读了许多类似的问答,但没有回应我的问题。谢谢

回答by Claudio Redi

No, C# doesn't support multiple inheritance. You could implement multiple interfaces but that's obviously not the same since interfaces can't have any concrete behavior implemented.

不,C# 不支持多重继承。您可以实现多个接口,但这显然不一样,因为接口不能实现任何具体的行为。

回答by Louis Kottmann

You can do this in C# via Mixins, it's implemented differently than multiple inheritance but in the end you do get an object with all the methods from several classes.

您可以通过 Mixins 在 C# 中执行此操作,它的实现方式与多重继承不同,但最终您确实获得了一个包含来自多个类的所有方法的对象。

It is not a feature of the language, but it can be found in third parties.

它不是语言的特性,但可以在第三方中找到。

You can read about it on my answer to this other question, or directly on re-mix's codeplex

您可以在我对另一个问题的回答中或直接在re-mix 的 codeplex上阅读它

A few points of interest:
- you can override methods in a Mixin, thus externally modifying the behavior of newly created classes.
- you can create scopes where classes are different while created within the scope.
- you need to create mixin'd objects via a factory (new MyObject() won't take Mixins into account, thankfully).

一些兴趣点:
- 您可以覆盖 Mixin 中的方法,从而从外部修改新创建的类的行为。
- 您可以在范围内创建不同的类时创建范围。
- 您需要通过工厂创建 mixin'd 对象(幸运的是,new MyObject() 不会考虑 Mixin)。

回答by MikeB

Have you tried using multiple interfaces instead? This might be a similar situation: Multiple derived abstract classes?

您是否尝试过使用多个接口?这可能是类似的情况: 多个派生抽象类?

回答by Adam Robinson

That specifically? No, as multiple inheritance is not supported in C#.

那个具体?不可以,因为 C# 不支持多重继承。

The closest you could get would be using an interface that combined two others:

你能得到的最接近的是使用一个结合了另外两个的界面:

public interface ISomeInterfaceA
{
    int Prop { get; }
}

public interface ISomeInterfaceB
{
    void SomeMethod();
}

public interface ICombined : ISomeInterfaceA, ISomeInterfaceB
{

}

Then you could require arguments of type ICombined, but this doesn't completely get what you're after. For instance, if I were to do this:

然后你可能需要 type 的参数ICombined,但这并不能完全得到你想要的。例如,如果我要这样做:

public class A : ISomeInterfaceA, ISomeInterfaceB
{
  ...
}

This wouldn't qualify, since I didn't explicitly implement the combined interface, just the two interfaces that it's composed of.

这不符合条件,因为我没有明确实现组合接口,只是它组成的两个接口。

回答by aserwin

If you are willing to implement the functionality in the final class, you can create interfaces and implement as many as you want...

如果您愿意在最终类中实现该功能,则可以创建接口并实现任意数量的接口...

回答by MitsakosGR

As of C# 8.0 on .NET Core 3.0, C# supports default implementations for Interface members.

从 .NET Core 3.0 上的 C# 8.0 开始,C# 支持接口成员的默认实现

public interface ILight
{
    void SwitchOn();
    void SwitchOff();
    bool IsOn();
}

// Basic Implementation
public class OverheadLight : ILight
{
    private bool isOn;
    public bool IsOn() => isOn;
    public void SwitchOff() => isOn = false;
    public void SwitchOn() => isOn = true;

    public override string ToString() => $"The light is {(isOn ? "on" : "off")}";
}

// Interface with default implementation
public interface ITimerLight : ILight
{
    // defines default implementation
    public async Task TurnOnFor(int duration)
    {
        Console.WriteLine("Using the default interface method for the ITimerLight.TurnOnFor.");
        SwitchOn();
        await Task.Delay(duration);
        SwitchOff();
        Console.WriteLine("Completed ITimerLight.TurnOnFor sequence.");
    }
}

// Implementation with interface
public class OverheadLight : ITimerLight
{
    private bool isOn;
    public bool IsOn() => isOn;
    public void SwitchOff() => isOn = false;
    public void SwitchOn() => isOn = true;

    public override string ToString() => $"The light is {(isOn ? "on" : "off")}";

    // Automatically gets the interface implementation
    //
    // To provide your own implementation: (no use of override)
    // public async Task TurnOnFor(int duration)
    // {
    // }
}