来迟了 楼上2位都是讲 理论 我来给楼主贴个例子吧:
public class BaseClass
{
protected string _className = "BaseClass";
public virtual void PrintName()
{
Console.WriteLine("Class Name: {0}", _className);
}
}
class DerivedClass : BaseClass
{
public string _className = "DerivedClass";
public override void PrintName()
{
Console.Write("The BaseClass Name is {0}");
//调用父类方法
base.PrintName();
Console.WriteLine("This DerivedClass is {0}", this._className);
}
}
class test1
{
public static void Main()
{
DerivedClass dc = new DerivedClass();
dc.PrintName();
Console.ReadKey();
}
}
BaseClass = 父类 , DerivedClass = 子类 |