从 C# 中的两个类继承

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

inherit from two classes in C#

c#.net

提问by Virus

Possible Duplicate:
Multiple Inheritance in C#

可能的重复:
C# 中的多重继承

I have two classes Class A and Class B. These two classes cannot inherit each other. I am creating new class called Class C. Now, I want to implement the methods in class A and class B by inheriting. I am aware that multiple inheritance is not possible in C# but is there any other way to do this?

我有两个类 Class A 和 Class B。这两个类不能相互继承。我正在创建名为 Class C 的新类。现在,我想通过继承来实现 A 类和 B 类中的方法。我知道在 C# 中不可能实现多重继承,但还有其他方法可以做到这一点吗?

Thanks

谢谢

采纳答案by KF2

Multitiple inheritance is not possible in C#, however it can be simulated using interfaces, see Simulated Multiple Inheritance Pattern for C#.

C# 中不可能进行多组继承,但是可以使用接口模拟它,请参阅C# 的模拟多重继承模式

The basic idea is to define an interface for the members on class Bthat you wish to access (call it IB), and then have Cinherit from Aand implement IBby internally storing an instance of B, for example:

基本思想是为B您希望访问的类上的成员定义一个接口(调用它IB),然后通过内部存储 的实例来C继承A和实现,例如:IBB

class C : A, IB
{
    private B _b = new B();

    // IB members
    public void SomeMethod()
    {
        _b.SomeMethod();
    }
}

There are also a couple of other alternaitve patterns explained on that page.

该页面上还解释了其他几种替代模式。

回答by Tigran

You can define a base class for Aand Bwhere you can hold a commonmethods/properties/fields of those.

您可以定义一个基类A,并B在那里你可以持有一个共同的方法/属性/那些领域。

After implement C:Base.

实施后C:Base

Or in order to simulate multiple inheritance, define a common interface(s) and implement them in C

或者为了模拟多重继承,定义一个公共interface(s)并在C

Hope this helps.

希望这可以帮助。

回答by Rich

An common alternative to inheritance is delegation (also called composition): X "has a" Y rather than X "is a" Y. So if A has functionality for dealing with Foos, and B has functionality for dealing with Bars, and you want both in C, then something like this:

继承的一个常见替代方法是委托(也称为组合):X“具有”Y 而不是 X“是”Y。因此,如果 A 具有处理 Foos 的功能,而 B 具有处理 Bars 的功能,并且您想要都在 C 中,然后是这样的:

public class A() {
  private FooManager fooManager = new FooManager(); // (or inject, if you have IoC)

  public void handleFoo(Foo foo) {
    fooManager.handleFoo(foo);
  }
}

public class B() {
  private BarManager barManager = new BarManager(); // (or inject, if you have IoC)

  public void handleBar(Bar bar) {
    barManager.handleBar(bar);
  }
}

public class C() {
  private FooManager fooManager = new FooManager(); // (or inject, if you have IoC)
  private BarManager barManager = new BarManager(); // (or inject, if you have IoC)

  ... etc
}

回答by CodeCaster

Use composition:

使用成分

class ClassC
{
    public ClassA A { get; set; }
    public ClassB B { get; set; }   

    public C (ClassA  a, ClassB  b)
    {
        this.A = a;
        this.B = b;
    }
}

Then you can call C.A.DoA(). You also can change the properties to an interface or abstract class, like public InterfaceA Aor public AbstractClassA A.

然后就可以调用了C.A.DoA()。您还可以将属性更改为接口或抽象类,例如public InterfaceA Apublic AbstractClassA A

回答by Rawling

If you want to literally use the method code from Aand Byou can make your Cclass contain an instance of each. If you code against interfaces for Aand Bthen your clients don't need to know you're giving them a Crather than an Aor a B.

如果你想从字面上使用方法代码AB你可以让你的C类包含每个的实例。如果您针对接口进行编码AB然后您的客户不需要知道您给他们的是一个C而不是一个A或一个B

interface IA { void SomeMethodOnA(); }
interface IB { void SomeMethodOnB(); }
class A : IA { void SomeMethodOnA() { /* do something */ } }
class B : IB { void SomeMethodOnB() { /* do something */ } }
class C : IA, IB
{
    private IA a = new A();
    private IB b = new B();
    void SomeMethodOnA() { a.SomeMethodOnA(); }
    void SomeMethodOnB() { b.SomeMethodOnB(); }
}

回答by Seph

Make two interfaces IAand IB:

制作两个接口IAIB

public interface IA
{
    public void methodA(int value);
}

public interface IB
{
    public void methodB(int value);
}

Next make Aimplement IAand Bimplement IB.

接下来 makeA执行IAB执行IB

public class A : IA
{
    public int fooA { get; set; }
    public void methodA(int value) { fooA = value; }
}

public class B : IB
{
    public int fooB { get; set; }
    public void methodB(int value) { fooB = value; }
}

Then implement your C class as follows:

然后按如下方式实现您的 C 类:

public class C : IA, IB
{
    private A _a;
    private B _b;

    public C(A _a, B _b)
    {
        this._a = _a;
        this._b = _b;
    }

    public void methodA(int value) { _a.methodA(value); }
    public void methodB(int value) { _b.methodB(value); }
}

Generally this is a poor design overall because you can have both Aand Bimplement a method with the same name and variable types such as foo(int bar)and you will need to decide how to implement it, or if you just call foo(bar)on both _aand _b. As suggested elsewhere you should consider a .Aand .Bproperties instead of combining the two classes.

一般来说,这是一个整体的设计不良,因为你可以同时拥有AB实施具有相同的名称和可变类型,如一个方法foo(int bar),你将需要决定如何实现它,或者如果你只需要调用foo(bar)两个_a_b。正如其他地方所建议的那样,您应该考虑 a.A.B属性,而不是将这两个类组合在一起。

回答by HatSoft

Do you mean you want Class C to be the base class for A & B in that case.

在这种情况下,您的意思是您希望 C 类成为 A 和 B 的基类。

public abstract class C
{
    public abstract void Method1();

    public abstract void Method2();
}

public class A : C
{
    public override void Method1()
    {
        throw new NotImplementedException();
    }

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


public class B : C
{
    public override void Method1()
    {
        throw new NotImplementedException();
    }

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