1 回答

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}";
}
}
}
添加回答
舉報