时间:2019-03-17 标签:c#多重继承
我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种特殊情况下,您不需要多重继承:如果类
C
仅从B
继承,则类C
的任何实例都可以转换为两者B
和A
;由于B
已经从A
派生,因此C
不需要再次从A
派生:(与其他人一样已经说过,如果您需要类似于多重继承的东西,请使用接口,因为一个类可以实现您想要的任意数量的接口。)
You do not need multiple inheritance in this particular case: If class
C
inherits only fromB
, any instance of classC
can be cast to bothB
andA
; sinceB
already derives fromA
,C
doesn't need to be derived fromA
again:(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.)
不可以。C# 不支持类的多重继承。
一个类可以继承一个类,也可以实现多个接口。
No. C# does not support multiple inheritance of classes.
A class may inherit from one class, and may also implement multiple interfaces.
这在 C# 中是不可能的,但是,您还应该考虑这是否是您真正想要的场景。
C真的是A和B吗?或者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.
您可以使用接口进行多重继承。
you can use Interfaces for multiple inheritance.
C# 不支持多重继承,但正如其他人建议的那样,您可以使用多个接口。然而,即使使用接口,有时您也想在同一个类中重用多个接口实现,这又确实需要多重继承。
为了解决这个问题(尽管它几乎从来不需要)我想出了一个模拟使用部分类组合的多重继承方法 和 T4 文本模板。这是一个小技巧,但它比在许多派生类中复制/粘贴相同的接口实现要好。下面是一个简化的示例:
IInterface.cs
InterfaceImpl.cs
BaseClass.cs
DerivedClassA.cs
DerivedClassAInterfaceImpl.tt
DerivedClassB.cs
DerivedClassBInterfaceImpl.tt
有点烦人的是,这需要为每个想要使用接口实现的派生类创建一个 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
InterfaceImpl.cs
BaseClass.cs
DerivedClassA.cs
DerivedClassAInterfaceImpl.tt
DerivedClassB.cs
DerivedClassBInterfaceImpl.tt
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.