class Program { static void Main(string[] args) { ParentClass test = new SubClass(); test.doSomething(); /*结果: ParentClass SubClass doSomething in ParentClass */ } } class ParentClass { public ParentClass() { Console.WriteLine("ParentClass"); } public virtual void doSomething() { Console.WriteLine("doSomething in ParentClass"); } } class SubClass:ParentClass { public SubClass() { Console.WriteLine("SubClass"); } public new void doSomething() { Console.WriteLine("doSomething in SubClass"); } }
class Program { static void Main(string[] args) { ParentClass test = new SubClass(); test.doSomething(); /* 结果: ParentClass SubClass doSomething in SubClass */ } } class ParentClass { public ParentClass() { Console.WriteLine("ParentClass"); } public virtual void doSomething() { Console.WriteLine("doSomething in ParentClass"); } } class SubClass:ParentClass { public SubClass() { Console.WriteLine("SubClass"); } public override void doSomething() { Console.WriteLine("doSomething in SubClass"); } }
时间: 2024-10-12 04:22:37