• 函數覆蓋總結知識點

    時間:2022-06-25 00:21:05 總結范文 我要投稿
    • 相關推薦

    函數覆蓋總結知識點

      一個虛函數被覆蓋后,任何父類變量都不能訪問該虛函數的具體實現。

    函數覆蓋總結知識點

      public virtual void IntroduceMyself(){...}//父類虛函數

      public new void IntroduceMyself(){...}//子類覆蓋父類虛函數

      using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MethodOverrideByNew{ public enum Genders { Female=0, Male=1 } public class Person { protected string _name; protected int _age; protected Genders _gender; ///

      /// 父類構造函數 ///

      public Person() { this._name = "DefaultName"; this._age = 23; this._gender = Genders.Male; } ///

      /// 定義虛函數IntroduceMyself() ///

      public virtual void IntroduceMyself() { System.Console.WriteLine("Person.IntroduceMyself()"); } ///

      /// 定義虛函數PrintName() ///

      public virtual void PrintName() { System.Console.WriteLine("Person.PrintName()"); } } public class ChinesePerson :Person{ ///

      /// 子類構造函數,指明從父類無參構造函數調用起 ///

      public ChinesePerson() :base(){ this._name = "DefaultChineseName"; } ///

      /// 覆蓋父類方法IntroduceMyself,使用new關鍵字修飾虛函數 ///

      public new void IntroduceMyself() { System.Console.WriteLine("ChinesePerson.IntroduceMyself()"); } ///

      /// 重載父類方法PrintName,使用override關鍵字修飾虛函數 ///

      public override void PrintName(){ System.Console.WriteLine("ChinesePerson.PrintName()"); } } class Program { static void Main(string[] args) { //定義兩個對象,一個父類對象,一個子類對象 Person aPerson = new ChinesePerson(); ChinesePerson cnPerson = new ChinesePerson(); //調用覆蓋的方法,父類對象不能調用子類覆蓋過的方法,只能調用自身的虛函數方法 aPerson.IntroduceMyself(); cnPerson.IntroduceMyself(); //調用重載方法,父類對象和子類對象都可以調用子類重載過后的方法 aPerson.PrintName(); cnPerson.PrintName(); System.Console.ReadLine(); } }}

      結果:

      Person.IntroduceMyself()

      ChinesePerson.IntroduceMyself()

      ChinesePerson.PrintName()

      ChinesePerson.PrintName()

      以上這篇C# 函數覆蓋總結學習(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

    【函數覆蓋總結知識點】相關文章:

    初中函數知識點總結07-29

    關于函數與方程的知識點總結10-17

    二次函數知識點總結07-03

    函數中自變量的知識點總結08-02

    二次函數知識點總結12-19

    初中數學所有函數的知識點總結11-22

    高二數學函數公式知識點總結06-28

    高中函數基本性質知識點總結07-25

    高一數學函數知識點總結12-01

    高中數學函數知識點最新總結07-02

    黄频国产免费高清视频_富二代精品短视频在线_免费一级无码婬片aa_精品9E精品视频在线观看