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());

}
}

转载于:https://blog.51cto.com/14152780/2336412

你可能感兴趣的:(C# 中使用 override 和 new 关键字进行版本控制的代码)