C#多重继承 [英] c# multiple inheritance
本文介绍了C#多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在C#
(伪)
class A;
class B : A;
class C : A, B;
...
A ac = (A)c;
...
B bc = (B)c;
这可能吗?
Is this possible?
推荐答案
您并不需要多重继承在这种特殊情况下:如果类 C
从 B $ C $既继承C>,类的任何实例
C
可以转换到两个 B
和 A
;因为 B
已经从 A
,派生ç
不需要从 A衍生
又说:
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.)
这篇关于C#多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文