다음은 C#에서 단일 상속의 예입니다. 예제에서 기본 클래스는 아버지이고 다음 코드 스니펫과 같이 선언됩니다. -
class Father {
public void Display() {
Console.WriteLine("Display");
}
} 우리의 파생 클래스는 Son이며 아래에 선언됩니다 -
class Son : Father {
public void DisplayOne() {
Console.WriteLine("DisplayOne");
}
} 예시
다음은 C#에서 단일 상속을 구현하는 완전한 예입니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAplication {
class Demo {
static void Main(string[] args) {
// Father class
Father f = new Father();
f.Display();
// Son class
Son s = new Son();
s.Display();
s.DisplayOne();
Console.ReadKey();
}
class Father {
public void Display() {
Console.WriteLine("Display");
}
}
class Son : Father {
public void DisplayOne() {
Console.WriteLine("DisplayOne");
}
}
}
} 출력
Display Display DisplayOne