第六章 上机2

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarRun
{
    public class Vehicle
    {
        //带参构造函数
        public Vehicle(string place, string type)
        {
            this.Place = place;
            this.Type = type;
        }
        //属性
        public string Place { get; set; }
        public string Type { get; set; }

        //方法
        public void VehicleRun()
        {
            Console.WriteLine("汽车在奔跑");
        }
    }
}
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarRun
{
    class Truck:Vehicle
    {
        //卡车带参构造函数
        public Truck(string place, string type)
            : base(place, type)
        {

        }
        //卡车奔跑的方法
        public void TruckRun()
        {
            Console.WriteLine("型号为{0},产地为{1}的卡车正在行驶",this.Type,base.Place);
        }
    }

}
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarRun
{
    class Program
    {
        static void Main(string[] args)
        {
            //父类对象
            Vehicle v = new Vehicle("中国", "现代");
            v.VehicleRun();

            //子类对象
            Truck truck = new Truck("中国", "红旗");
            truck.TruckRun();//子类方法
            truck.VehicleRun();//父类方法
            Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/wuayn/p/8810272.html