雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

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

一個(gè)虛函數(shù)被覆蓋后,任何父類變量都不能訪問(wèn)該虛函數(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ù),指明從父類無(wú)參構(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)
    {
      //定義兩個(gè)對(duì)象,一個(gè)父類對(duì)象,一個(gè)子類對(duì)象
      Person aPerson = new ChinesePerson();
      ChinesePerson cnPerson = new ChinesePerson();
      //調(diào)用覆蓋的方法,父類對(duì)象不能調(diào)用子類覆蓋過(guò)的方法,只能調(diào)用自身的虛函數(shù)方法
      aPerson.IntroduceMyself();   
      cnPerson.IntroduceMyself();
      //調(diào)用重載方法,父類對(duì)象和子類對(duì)象都可以調(diào)用子類重載過(guò)后的方法
      aPerson.PrintName();
      cnPerson.PrintName();

      System.Console.ReadLine();
    }
  }
}

結(jié)果:

Person.IntroduceMyself()

ChinesePerson.IntroduceMyself()

ChinesePerson.PrintName()

ChinesePerson.PrintName()

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

上一篇:分享C#中幾個(gè)可用的類

欄    目:C#教程

下一篇:淺析C#中StringBuilder類的高效及與String的對(duì)比

本文標(biāo)題:C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

本文地址:http://m.jygsgssxh.com/a1/C_jiaocheng/6510.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有