Nhiều lớp được kế thừa từ lớp cơ sở trong Kế thừa phân cấp.
Trong ví dụ, lớp cơ sở của chúng ta là Father -
class Father {
public void display() {
Console.WriteLine("Display...");
}
} Nó có Con trai và Con gái là lớp dẫn xuất. Hãy để chúng tôi làm thế nào để thêm một lớp dẫn xuất trong Kế thừa -
class Son : Father {
public void displayOne() {
Console.WriteLine("Display One");
}
} Ví dụ
Ví dụ đầy đủ sau đây về việc triển khai Kế thừa phân cấp trong 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");
}
}
}
}