C# 分层继承示例
csharpserver side programmingprogramming更新于 2024/9/24 20:04:00
在分层继承中,从基类继承了多个类。
在示例中,我们的基类是 Father −
class Father { public void display() { Console.WriteLine("Display..."); } }
它有 Son 和 Daughter 作为派生类。让我们看看如何在继承 − 中添加派生类
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"); } } } }