Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c# multiple inheritance

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...