using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 多态 { class Program { static void Main(string[] args) { //员工9点打卡, 经理11点打卡,程序员不打卡 Employee emp = new Employee(); emp.Daka(); Manager man = new Manager(); man.Daka(); Programer pro = new Programer(); pro.Daka(); Console.WriteLine(); //和上面的一样 Employee[] emps = { emp, man, pro }; for (int i = 0; i < emps.Length;i++ ) { if(emps[i] is Manager) { ((Manager)emps[i]).Daka(); } else if(emps[i] is Programer) { ((Programer)emps[i]).Daka(); } else { emps[i].Daka(); } } Console.ReadKey(); } } class Employee { public void Daka() { Console.WriteLine("9点打卡"); } } class Manager:Employee { public void Daka() { Console.WriteLine("11点打卡"); } } class Programer:Employee { public void Daka() { Console.WriteLine("不打卡"); } } }
可见上述方式造成了很大的代码冗余
为了减少代码冗余,我们使用虚方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 多态 { class Program { static void Main(string[] args) { //员工9点打卡, 经理11点打卡,程序员不打卡 Employee emp = new Employee(); emp.Daka(); Manager man = new Manager(); man.Daka(); Programer pro = new Programer(); pro.Daka(); Console.WriteLine(); //和上面的一样 Employee[] emps = { emp, man, pro }; for (int i = 0; i < emps.Length;i++ ) { //if(emps[i] is Manager) //{ // ((Manager)emps[i]).Daka(); //} //else if(emps[i] is Programer) //{ // ((Programer)emps[i]).Daka(); //} //else //{ // emps[i].Daka(); //} emps[i].Daka(); } Console.ReadKey(); } } class Employee { public virtual void Daka() { Console.WriteLine("9点打卡"); } } class Manager:Employee { public override void Daka() { Console.WriteLine("11点打卡"); } } class Programer:Employee { public override void Daka() { Console.WriteLine("不打卡"); } } }
运行结果同上面一样
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 多态 { class Program { static void Main(string[] args) { //员工9点打卡, 经理11点打卡,程序员不打卡 Employee emp = new Employee(); emp.Daka(); Manager man = new Manager(); man.Daka(); Programer pro = new Programer(); pro.Daka(); Console.WriteLine(); //和上面的一样 Employee[] emps = { emp, man, pro }; for (int i = 0; i < emps.Length;i++ ) { //if(emps[i] is Manager) //{ // ((Manager)emps[i]).Daka(); //} //else if(emps[i] is Programer) //{ // ((Programer)emps[i]).Daka(); //} //else //{ // emps[i].Daka(); //} emps[i].Daka(); emps[i].Say(); } //抽象类不能创建对象 //Person per = new Person(); Console.ReadKey(); } } abstract class Person { //抽象类不能创建对象 //抽象类中可以含有字段、实函数和虚函数 public string Name { get; set; } public int Age { get; set; } public void Say() { Console.WriteLine("hello"); } //虚函数,可以实现,可以不实现 public virtual void Daka() { Console.WriteLine("virtual hello"); } } class Employee:Person { //public override void Daka() //{ // Console.WriteLine("9点打卡"); //} //没有实现时运行Person中的virtual hello } class Manager:Employee { public override void Daka() { Console.WriteLine("11点打卡"); } } class Programer:Employee { public override void Daka() { Console.WriteLine("不打卡"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 多态 { class Program { static void Main(string[] args) { //员工9点打卡, 经理11点打卡,程序员不打卡 Employee emp = new Employee(); emp.Daka(); Manager man = new Manager(); man.Daka(); Programer pro = new Programer(); pro.Daka(); Console.WriteLine(); //和上面的一样 Employee[] emps = { emp, man, pro }; for (int i = 0; i < emps.Length;i++ ) { //if(emps[i] is Manager) //{ // ((Manager)emps[i]).Daka(); //} //else if(emps[i] is Programer) //{ // ((Programer)emps[i]).Daka(); //} //else //{ // emps[i].Daka(); //} emps[i].Daka(); emps[i].Say(); } //抽象类不能创建对象 //Person per = new Person(); Console.ReadKey(); } } abstract class Person { //抽象类不能创建对象 //抽象类中可以含有字段、实函数和虚函数 public string Name { get; set; } public int Age { get; set; } public void Say() { Console.WriteLine("hello"); } //虚函数,可以实现,可以不实现 public virtual void Daka() { Console.WriteLine("virtual hello"); } //如果用户定义了自己的构造函数,而在子类继承时调用的是原先的默认的无参构造函数 //因此要再定义一个无参的构造函数 public Person(string name,int age) { this.Name = name; this.Age = age; } public Person() { } } class Employee:Person { //public override void Daka() //{ // Console.WriteLine("9点打卡"); //} //没有实现时运行Person中的virtual hello } class Manager : Employee { public override void Daka() { Console.WriteLine("11点打卡"); } } class Programer : Employee { public override void Daka() { Console.WriteLine("不打卡"); } } }