多級繼承的 C# 示例


當派生類由另一個派生類形成時就會發生多級繼承。

在 C# 中,祖父、父親和兒子是表示多級繼承的完美示例 −

示例

以下是說明在 C# 中使用多級繼承的示例。

即時演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class Son : Father {
      public void DisplayTwo() {
         Console.WriteLine("Son.. ");
      }
      static void Main(string[] args) {
         Son s = new Son();
         s.Display();
         s.DisplayOne();
         s.DisplayTwo();
         Console.Read();
      }
   }
   class Grandfather {
      public void Display() {
         Console.WriteLine("Grandfather...");
      }
   }
   class Father : Grandfather {
      public void DisplayOne() {
         Console.WriteLine("Father...");
      }
   }
}

輸出

Grandfather...
Father...
Son.. 

更新日期:2020-06-19

4K+ 瀏覽量

開始你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.