C#之多态与继承汽车类

namespace ConsoleApplication10
{
    //汽车类
   class Vehicle
    {
       public virtual void vehicleRun(){
            Console.WriteLine("汽车在行驶");
        }
}
     //卡车类
       class Truck:Vehicle {
           public override void vehicleRun()
           {
                Console.WriteLine("卡车在行使");
                
            }

           
           
       }
           
           //微型卡车类
            class SmallTruck:Truck {

                public override void vehicleRun()
                {
                    Console.WriteLine("微型卡车在行驶!");
                }                              
            }

    //main函数
            class Program
            {
                static void Main(string[] args)
                {
                    //汽车类
                    Vehicle s1 = new Vehicle();
                    s1.vehicleRun();

                    //卡车类
                    Vehicle s2 = new Truck();
                    s2.vehicleRun();

                    //微型卡车类
                    Vehicle s3 = new SmallTruck();
                    s3.vehicleRun();

                    Console.WriteLine("请按任意键继续....");
                    Console.Read();

                }
            }




}
   


C#之多态与继承汽车类_第1张图片

你可能感兴趣的:(c#)