亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想要設計一個交通工具類Vehicle,并且包含以下的內容,請問該怎么實現?

想要設計一個交通工具類Vehicle,并且包含以下的內容,請問該怎么實現?

桃花長相依 2022-05-04 11:07:33
設計一個交通工具類Vehicle,包含的數據成員有車輪個數wheels和車重weight。以及帶有這兩個參數的構造方法,具有Run方法,Run中方法輸出running字樣。小車類Car是它的子類,其中增加了數據成員車載人數passenger_load。Car類中有帶參數的構造方法,重寫Run方法:輸出Car is running。并重寫ToString()方法:顯示car中信息(顯示車輪數、車重、車載人數)。最后編寫主方法,定義car的對象,并調用Run方法,以及顯示car中信息。
查看完整描述

1 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

使用開發工具為VS2017.針對你問題相關代碼如下:

using System;


namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            Car car = new Car(4,1000,5);

            car.Run();

            Console.WriteLine(car.ToString());

            Console.Read();

        }

    }

    /// <summary>

    /// 交通工具類

    /// </summary>

    public class Vehicle

    {

        /// <summary>

        /// 車輪

        /// </summary>

        public int wheels { get; }

        /// <summary>

        /// 車重

        /// </summary>

        public int weight { get; }


        /// <summary>

        /// 構造方法

        /// </summary>

        /// <param name="wheels"></param>

        /// <param name="weight"></param>

        public Vehicle(int wheels,int weight)

        {

            this.weight = weight;

            this.wheels = wheels;

        }

        public virtual void Run()

        {

            Console.WriteLine("running");

        }


    }

    public class Car: Vehicle

    {

        /// <summary>

        /// 車載人數

        /// </summary>

        public int passenger_load { get; }

        public Car(int wheels, int weight,int passenger_load) :base(wheels,weight)

        {

            this.passenger_load = passenger_load;

        }


        public override void Run()

        {

            Console.WriteLine("Car is running");

        }


        public override string ToString()

        {

            return $"車輪:{wheels}、車重:{weight}、車載人數:{passenger_load}";

        }

    }

}



查看完整回答
反對 回復 2022-05-09
  • 1 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號