时间:2019-03-17 标签:c#多重继承

发布于 2024-08-21 08:38:50 字数 152 浏览 5 评论 0原文

我想用 C#

(伪代码)

class A;

class B : A;

class C : A, B;

...

A ac = (A)c;

...

B bc = (B)c;

实现这一点,这可能吗?

I would like to achieve this in C#

(Pseudocode)

class A;

class B : A;

class C : A, B;

...

A ac = (A)c;

...

B bc = (B)c;

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生寂 2024-08-28 08:38:50

在这种特殊情况下,您不需要多重继承:如果类 C 仅从 B 继承,则类 C 的任何实例都可以转换为两者B A;由于 B 已经从 A 派生,因此 C 不需要再次从 A 派生:(

class A      { ... }

class B : A  { ... }

class C : B  { ... }

...

C c = new C();
B bc = (B)c;    // <-- will work just fine without multiple inheritance
A ac = (A)c;    // <-- ditto

与其他人一样已经说过,如果您需要类似于多重继承的东西,请使用接口,因为一个类可以实现您想要的任意数量的接口。)

You do not need multiple inheritance in this particular case: If class C inherits only from B, any instance of class C can be cast to both B and A; since B already derives from A, C doesn't need to be derived from A again:

class A      { ... }

class B : A  { ... }

class C : B  { ... }

...

C c = new C();
B bc = (B)c;    // <-- will work just fine without multiple inheritance
A ac = (A)c;    // <-- ditto

(As others have already said, if you need something akin to multiple inheritance, use interfaces, since a class can implement as many of those as you want.)

心作怪 2024-08-28 08:38:50

不可以。C# 不支持类的多重继承。

一个类可以继承一个类,也可以实现多个接口。

No. C# does not support multiple inheritance of classes.

A class may inherit from one class, and may also implement multiple interfaces.

如梦初醒的夏天 2024-08-28 08:38:50

这在 C# 中是不可能的,但是,您还应该考虑这是否是您真正想要的场景。

C真的是AB吗?或者C一个A和一个B
如果后者是正确的,则应该使用组合而不是继承。

It is not possible in C#, but, you should also think if this is the scenario you really want.

Is C really an A and a B ? Or does C has an A and a B.
If the latter is true, you should use composition instead of inheritance.

无需解释 2024-08-28 08:38:50

您可以使用接口进行多重继承。

you can use Interfaces for multiple inheritance.

陈年往事 2024-08-28 08:38:50

C# 不支持多重继承,但正如其他人建议的那样,您可以使用多个接口。然而,即使使用接口,有时您也想在同一个类中重用多个接口实现,这又确实需要多重继承。

为了解决这个问题(尽管它几乎从来不需要)我想出了一个模拟使用部分类组合的多重继承方法 和 T4 文本模板。这是一个小技巧,但它比在许多派生类中复制/粘贴相同的接口实现要好。下面是一个简化的示例:

IInterface.cs

public interface IInterface
{
    void Foo();
}

InterfaceImpl.cs

public partial class InterfaceImpl : IInterface
{
    public void Foo()
    {
        Console.WriteLine("Foo");
    }
}

BaseClass.cs

using System;

public class BaseClass
{
    public void Bar()
    {
        Console.WriteLine("Bar");
    }
}

DerivedClassA.cs

public partial class DerivedClassA : BaseClass, IInterface
{
    public void FooBar()
    {
        this.Foo();
        this.Bar();
    }
}

DerivedClassAInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassA"); #>
<#= codeText #>

DerivedClassB.cs

 public partial class DerivedClassB : BaseClass, IInterface
    {
        public void BarFoo()
        {
            this.Bar();
            this.Foo();
        }
    }

DerivedClassBInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassB"); #>
<#= codeText #>

有点烦人的是,这需要为每个想要使用接口实现的派生类创建一个 tt 文件,但如果 InterfaceImpl.cs 超过几行代码,它仍然可以节省复制/粘贴代码。也可能只创建一个 tt 文件并指定所有派生部分类的名称并生成 多个文件

我在类似的问题中提到过这种方法 这里

C# doesn't support multiple inheritance, but as others have suggested you can use multiple interfaces instead. However, even with interfaces sometimes you want to reuse multiple interface implementations in the same class, which again would really require multiple inheritance.

To get around this (even though it's almost never needed) I've come up with a simulated multiple inheritance approach using a combination of partial classes and T4 Text Templates. It's a slight hack but it's better than copy/pasting the same interface implementation in lots of derived classes. Below is a simplified example:

IInterface.cs

public interface IInterface
{
    void Foo();
}

InterfaceImpl.cs

public partial class InterfaceImpl : IInterface
{
    public void Foo()
    {
        Console.WriteLine("Foo");
    }
}

BaseClass.cs

using System;

public class BaseClass
{
    public void Bar()
    {
        Console.WriteLine("Bar");
    }
}

DerivedClassA.cs

public partial class DerivedClassA : BaseClass, IInterface
{
    public void FooBar()
    {
        this.Foo();
        this.Bar();
    }
}

DerivedClassAInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassA"); #>
<#= codeText #>

DerivedClassB.cs

 public partial class DerivedClassB : BaseClass, IInterface
    {
        public void BarFoo()
        {
            this.Bar();
            this.Foo();
        }
    }

DerivedClassBInterfaceImpl.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<# var codeText = System.IO.File.ReadAllText(this.Host.ResolvePath("InterfaceImpl.cs")).Replace("InterfaceImpl", "DerivedClassB"); #>
<#= codeText #>

It's a little annoying that this requires creating a tt file for each derived class that wants to use the interface implementation, but it still saves on copy/paste code if InterfaceImpl.cs is more than a few lines of code. It's also probably possible to just create one tt file and specify the name of all the derived partial classes and generate multiple files.

I've mentioned this approach in a similar question here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文