亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

 更新時間:2016年05月26日 16:17:51   投稿:jingxian  
下面小編就為大家?guī)硪黄狢# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

覆蓋類成員:通過new關(guān)鍵字修飾虛函數(shù)表示覆蓋該虛函數(shù)。

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

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

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

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;
    /// <summary>
    /// 父類構(gòu)造函數(shù)
    /// </summary>
    public Person() {
      this._name = "DefaultName";
      this._age = 23;
      this._gender = Genders.Male;
    }
    /// <summary>
    /// 定義虛函數(shù)IntroduceMyself()
    /// </summary>
    public virtual void IntroduceMyself() {
      System.Console.WriteLine("Person.IntroduceMyself()");
    }
    /// <summary>
    /// 定義虛函數(shù)PrintName()
    /// </summary>
    public virtual void PrintName() {
      System.Console.WriteLine("Person.PrintName()");
    }
  }
  public class ChinesePerson :Person{
    /// <summary>
    /// 子類構(gòu)造函數(shù),指明從父類無參構(gòu)造函數(shù)調(diào)用起
    /// </summary>
    public ChinesePerson() :base(){
      this._name = "DefaultChineseName";
    }
    /// <summary>
    /// 覆蓋父類方法IntroduceMyself,使用new關(guān)鍵字修飾虛函數(shù)
    /// </summary>
    public new void IntroduceMyself() {
      System.Console.WriteLine("ChinesePerson.IntroduceMyself()");
    }
    /// <summary>
    /// 重載父類方法PrintName,使用override關(guān)鍵字修飾虛函數(shù)
    /// </summary>
    public override void PrintName(){
      System.Console.WriteLine("ChinesePerson.PrintName()");      
    }
  }

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

      System.Console.ReadLine();
    }
  }
}

結(jié)果:

Person.IntroduceMyself()

ChinesePerson.IntroduceMyself()

ChinesePerson.PrintName()

ChinesePerson.PrintName()

以上這篇C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論