계층적 상속의 기본 클래스에서 둘 이상의 클래스가 상속되었습니다.
예시에서 기본 클래스는 Father입니다. -
class Father {
public void display() {
Console.WriteLine("Display...");
}
} 아들이 있습니다. 및 딸 파생 클래스로. Inheritance에 파생 클래스를 추가하는 방법을 알아보겠습니다 -
class Son : Father {
public void displayOne() {
Console.WriteLine("Display One");
}
} 예시
다음은 C#에서 계층적 상속을 구현하는 완전한 예 −
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance {
class Test {
static void Main(string[] args) {
Father f = new Father();
f.display();
Son s = new Son();
s.display();
s.displayOne();
Daughter d = new Daughter();
d.displayTwo();
Console.ReadKey();
}
class Father {
public void display() {
Console.WriteLine("Display...");
}
}
class Son : Father {
public void displayOne() {
Console.WriteLine("Display One");
}
}
class Daughter : Father {
public void displayTwo() {
Console.WriteLine("Display Two");
}
}
}
}