Multiple Inheritance in C#

后端 未结 15 1833
醉酒成梦
醉酒成梦 2020-11-22 03:03

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

15条回答
  •  攒了一身酷
    2020-11-22 03:20

    If X inherits from Y, that has two somewhat orthogonal effects:

    1. Y will provide default functionality for X, so the code for X only has to include stuff which is different from Y.
    2. Almost anyplace a Y would be expected, an X may be used instead.

    Although inheritance provides for both features, it is not hard to imagine circumstances where either could be of use without the other. No .net language I know of has a direct way of implementing the first without the second, though one could obtain such functionality by defining a base class which is never used directly, and having one or more classes that inherit directly from it without adding anything new (such classes could share all their code, but would not be substitutable for each other). Any CLR-compliant language, however, will allow the use of interfaces which provide the second feature of interfaces (substitutability) without the first (member reuse).

提交回复
热议问题