using System;
class MyBaseClass
{
virtual public void Print()
{Console.WriteLine("This is the 鸡 class.");}
}
class MyDerivedClass:MyBaseClass
{
override public void Print()
{Console.WriteLine("This is the derived 派生的 class.");}
}
class SecondDerived : MyDerivedClass {
new public void Print() { //这里关键字是override呢?
Console.WriteLine("This is the second derived class.");
}
}
class Program {
static void Main() // Main
{
SecondDerived derived = new SecondDerived(); // Use SecondDerived.
MyBaseClass mybc = derived; // Use MyBaseClass.
//也可以写成MyBaseClass mybc=(MyBaseClass)derived;
derived.Print();
mybc.Print();
}
}
C# new关键字和override
最新推荐文章于 2024-07-29 13:46:39 发布