分层继承的 C# 示例
在一个分层继承中,父类可继承为多个类。
在该示例中,我们的父类为 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"); } } } }
广告