C#, Java等语言都不支持类的多继承, 也就是说一个类如果已经继承了一个父类了, 那么它就不能再继承其他类了, 这样从某种程度上说丧失了一些重用性. 典型的例子是: ASP.NET中要求所有的控件都必须继承自UserControl, 这样我们自定义的控件就不能直接继承自我们自己的抽象类, 一般做法是在中间引入一个自己的控件基类, 即: MyControl -> MyControlBase -> UserControl.

Interfaces:
用内部类(Nested Class)来模拟多继承public interface ICar 
}

Base classes:
用内部类(Nested Class)来模拟多继承public class abstract CarBase : ICar
}


我们要造一辆同时具有Car和Plane的车, 但使用同一个油箱.

用内部类(Nested Class)来模拟多继承public partial class FlayableCar
}

使用
用内部类(Nested Class)来模拟多继承FlyableCar flyableCar = new FlyableCar();
用内部类(Nested Class)来模拟多继承
用内部类(Nested Class)来模拟多继承ICar car 
= flyableCar.Car;
用内部类(Nested Class)来模拟多继承car.Run();
用内部类(Nested Class)来模拟多继承
用内部类(Nested Class)来模拟多继承IPlane plane 
= flyableCar.Plane;
用内部类(Nested Class)来模拟多继承plane.Fly();

相关文章: