Nhiều Thừa kế không được hỗ trợ trong C #. Để triển khai nhiều kế thừa, hãy sử dụng Giao diện.
Đây là giao diện của chúng tôi PaintCost trong lớp Shape -
public interface PaintCost {
int getCost(int area);
} Hình dạng là lớp cơ sở của chúng ta trong khi Hình chữ nhật là lớp dẫn xuất -
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
} Bây giờ chúng ta hãy xem mã hoàn chỉnh để triển khai Giao diện cho nhiều thừa kế trong C # -
Using System;
namespace MyInheritance {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
public interface PaintCost {
int getCost(int area);
}
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
}
class RectangleDemo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(8);
Rect.setHeight(10);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}