练习:C# -- 多态
Posted 庚xiao午
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习:C# -- 多态相关的知识,希望对你有一定的参考价值。
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("程序员不打卡"); } } }
以上是关于练习:C# -- 多态的主要内容,如果未能解决你的问题,请参考以下文章