6.19 多态
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _6._19_多态 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 /*多态,有了继承才有多态 14 继承同一个父类的所有子类,使用起来都不一样*/ 15 Console.WriteLine("男人"); 16 men m = new men(); 17 m.chifan(); 18 m.xihuanchi(); 19 m.zuqiu(); 20 21 22 Console.WriteLine("女人"); 23 women w = new women(); 24 w.chifan(); 25 w.xihuanchi(); 26 w.guangjie(); 27 28 Console.WriteLine("小男孩"); 29 xiaomen x = new xiaomen(); 30 x.naifen(); 31 x.xihuanchi();//执行men 里面的语句,而不是ren 里面的语句 32 x.chifan(); 33 34 Console.ReadLine(); 35 36 37 } 38 } 39 }
第一父集 REN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _6._19_2_抽象类 { public abstract class ren { public abstract void chifan(); public abstract void hejiu(); public void huxi() { Console.WriteLine("人需要呼吸"); } public virtual void heshui() { Console.WriteLine("人需要喝水"); } public void yule() { Console.WriteLine("父集人需要娱乐"); } public abstract void pengyou() { Console.WriteLine("喜欢交朋友"); } } }
第二层 子集 nanren
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _6._19_2_抽象类 { class nanren : ren //子集不需要注明,类型是抽象类 { public override void chifan() { Console.WriteLine("喜欢吃肉"); } public override void hejiu() { Console.WriteLine("喜欢喝梦之蓝"); } public override void pengyou() { } } }
Women
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _6._19_多态 { class women:ren { public override void xihuanchi() { Console.WriteLine("我是女人,喜欢吃菜"); } public void guangjie() { Console.WriteLine("我爱逛街"); } } }
xiaoren
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _6._19_多态 { class xiaomen : men { public void naifen() { Console.WriteLine("喜欢吃奶粉"); } } }