使用virtual标记父类为虚方法,使用override重写子类
using System; namespace 多态 { class Program { static void Main(string[] args) { zhenDuck z = new zhenDuck(); muDuck m = new muDuck(); xpDuck x = new xpDuck(); //把三种叫法,放到字符串数组中 zhenDuck[] ducks = {z,m,x }; for (int i = 0; i < ducks.Length; i++) { ducks[i].Duck(); } Console.ReadKey(); } public class zhenDuck//构造函数,初始化对象依次赋值用 { public virtual void Duck()//使用virtual标记父类为虚方法 { Console.WriteLine("真鸭子嘎嘎叫"); } } public class muDuck : zhenDuck { public override void Duck()//使用override重写子类 { Console.WriteLine("木鸭子吱吱叫"); } } public class xpDuck : zhenDuck { public override void Duck() { Console.WriteLine("橡皮鸭子唧唧叫"); } } } }
标签:zhenDuck,void,多态,Console,class,override,之虚,方法,public From: https://www.cnblogs.com/chungeblog/p/17103536.html