C# 学习历程——类的封装,继承与多态
了解virtual,new,override的区别
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { class A { public virtual void Word() { Console.WriteLine("A"); } } class B : A { public new void Word() { Console.WriteLine("B"); } } class C : B { public virtual void Word() { Console.WriteLine("C"); } } class D : A { public override void Word() { Console.WriteLine("D"); } } static void Main(string[] args) { A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D(); A b2 = new B(); A c2 = new C(); A d2 = new D(); a1.Word(); b1.Word(); c1.Word(); d1.Word(); b2.Word(); c2.Word(); d2.Word(); } } }