C# 中使用 override 和 new 關鍵字進行版本控制的代碼

在研發之餘,把寫代碼過程中比較常用的代碼備份一下,下面的代碼內容是關於C# 中使用 override 和 new 關鍵字進行版本控制的代碼,應該是對大家有些用。

public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
public virtual string Meth3()
{
return "MyBase-Meth3";
}
}

class MyDerived : MyBase
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}

public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

  System.Console.WriteLine(mB.Meth1());
  System.Console.WriteLine(mB.Meth2());
  System.Console.WriteLine(mB.Meth3());

}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章