클래스는 둘 이상의 클래스 또는 인터페이스에서 파생될 수 있습니다. 즉, 여러 기본 클래스 또는 인터페이스에서 데이터와 기능을 상속할 수 있습니다.
예를 들어 다음 파생 클래스가 있는 차량 기본 클래스입니다.
Truck Bus Motobike
파생 클래스는 기본 클래스 멤버 변수와 멤버 메서드를 상속합니다.
같은 방법으로 Shape 클래스의 파생 클래스는 다음 예제와 같이 Rectangle이 될 수 있습니다.
예시
using System;
namespace Program {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape {
public int getArea() {
return (width * height);
}
}
class Demo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
} 출력
Total area: 35