C#学习 小知识_多态的简单实现_2018Oct

多态的实现三步骤

    1.父类   写入方法  (抽象类必须定义抽象方法)

    2.子类   继承父类  重写方法  (对父类抽象方法(或虚方法)进行重写)

    3.父类对子类重写的方法进行控制

class Program //Main函数实现
 {

      static void Main(string[] args)
     {
        Father FaBySon = new Son();
       Father faByDoughter = new Doughter();
       FaBySon.Method();
       faByDoughter.Method();
     Console.ReadKey();
     }
}
public abstract class Father //父类 抽象类
{

     public abstract void Method();
    public void Metho()
   {
        Console.WriteLine("Father");
    }
}
public class Son : Father//子类1
{
       public override void Method()
     {
          Console.WriteLine("Son");
     }
}
public class Doughter : Father//子类2
{
      public override void Method()
     {
     Console.WriteLine("Doughter");
     }
}

执行窗口如下:

原文地址:https://www.cnblogs.com/RainPaint/p/9803529.html