C# 不支持多重继承
多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:
实例
using System;namespace InheritanceApplication{class Shape{public void setWidth(int w){width = w;}public void setHeight(int h){height = h;}protected int width;protected int height;}// 基类 PaintCostpublic interface PaintCost{int getCost(int area);}// 派生类class Rectangle : Shape, PaintCost{public int getArea(){return (width * height);}public int getCost(int area){return area * 70;}}class RectangleTester{static void Main(string[] args){Rectangle Rect = new Rectangle();int area;Rect.setWidth(5);Rect.setHeight(7);area = Rect.getArea();// 打印对象的面积Console.WriteLine("总面积: {0}", Rect.getArea());Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));Console.ReadKey();}}}
