using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 多态 { class Program { static void Main(string[] args) { /* 练习: 真的鸭子嘎嘎叫 木头鸭子吱吱叫 橡胶鸭子唧唧叫 */ RealDark rrd = new RealDark(); WoodenDark wd = new WoodenDark(); RubberDark rd = new RubberDark(); Dark[] d = { rrd, wd, rd }; for (int i = 0; i < d.Length; i++) { d[i].Bark(); } Console.ReadLine(); /* 练习: 经理10点打卡 员工9点打卡 程序员不打卡 */ Employee em = new Employee(); Manager ma = new Manager(); Programmer pr = new Programmer(); Employee[] ee = { em, ma, pr }; for (int i = 0; i < ee.Length; i++) { ee[i].daka(); } Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 多态 { public class Dark { public virtual void Bark() { Console.WriteLine("我是鸭子"); } } public class RealDark : Dark { public override void Bark() { Console.WriteLine("真的鸭子嘎嘎叫~"); } } public class WoodenDark : Dark { public override void Bark() { Console.WriteLine("木头鸭子吱吱叫~"); } } public class RubberDark : Dark { public override void Bark() { Console.WriteLine("橡胶鸭子唧唧叫~"); } } public class Employee { public virtual void daka() { Console.WriteLine("员工9点打卡"); } } public class Manager : Employee { public override void daka() { Console.WriteLine("经理10点打卡"); } } public class Programmer : Employee { public override void daka() { Console.WriteLine("程序员不打卡"); } } }
时间: 2024-10-06 18:47:29