C# 單繼承示例


以下是 C# 中單繼承的示例。在示例中,基類為 Father,宣告如下所示 −

class Father {
   public void Display() {
      Console.WriteLine("Display");
   }
}

我們的派生類為 Son,如下面宣告 −

class Son : Father {
   public void DisplayOne() {
      Console.WriteLine("DisplayOne");
   }
}

示例

以下是 C# 中實現單繼承的完整示例。

動態演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAplication {
   class Demo {
      static void Main(string[] args) {
         // Father class
         Father f = new Father();
         f.Display();
         // Son class
         Son s = new Son();
         s.Display();
         s.DisplayOne();

         Console.ReadKey();
      }
      class Father {
         public void Display() {
            Console.WriteLine("Display");
         }
      }
      class Son : Father {
         public void DisplayOne() {
            Console.WriteLine("DisplayOne");
         }
      }
   }
}

輸出

Display
Display
DisplayOne

更新日期: 19-Jun-2020

3K+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.