想要设计一个交通工具类Vehicle,并且包含以下的内容,请问该怎么实现?

设计一个交通工具类Vehicle,包含的数据成员有车轮个数wheels和车重weight。以及带有这两个参数的构造方法,具有Run方法,Run中方法输出running字样。小车类Car是它的子类,其中增加了数据成员车载人数passenger_load。Car类中有带参数的构造方法,重写Run方法:输出Car is running。并重写ToString()方法:显示car中信息(显示车轮数、车重、车载人数)。最后编写主方法,定义car的对象,并调用Run方法,以及显示car中信息。

桃花长相依
浏览 193回答 1
1回答

幕布斯6054654

使用开发工具为VS2017.针对你问题相关代码如下:using System;namespace ConsoleApp1{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Car car = new Car(4,1000,5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.Run();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(car.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Read();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// 交通工具类&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public class Vehicle&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// 车轮&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; public int wheels { get; }&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// 车重&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; public int weight { get; }&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// 构造方法&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; /// <param name="wheels"></param>&nbsp; &nbsp; &nbsp; &nbsp; /// <param name="weight"></param>&nbsp; &nbsp; &nbsp; &nbsp; public Vehicle(int wheels,int weight)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.weight = weight;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.wheels = wheels;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public virtual void Run()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("running");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Car: Vehicle&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// 车载人数&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; public int passenger_load { get; }&nbsp; &nbsp; &nbsp; &nbsp; public Car(int wheels, int weight,int passenger_load) :base(wheels,weight)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.passenger_load = passenger_load;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public override void Run()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Car is running");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public override string ToString()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $"车轮:{wheels}、车重:{weight}、车载人数:{passenger_load}";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP