在“层次继承”中,从基类继承多个类。
在示例中,我们的基类是父亲-
class Father { public void display() { Console.WriteLine("Display..."); } }
它具有儿子和女儿作为派生类。让我们如何在继承中添加派生类-
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"); } } } }