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

更新於: 2020-6-19

3K+ 瀏覽

開始您的事業

完成課程後獲得認證

開始
廣告
© . All rights reserved.