C#编程:设计⼀个交通⼯具类Vehicle,包含的数据成员有车轮个数wheels和车
重weigh
设计⼀个交通⼯具类Vehicle,包含的数据成员有车轮个数wheels和车重weight。以及带有这两个参数的构造⽅法,具有Run⽅法,Run中⽅法输出running字样。⼩车类Car是它的⼦类,其中增加了数据成员车载⼈数passenger_load。Car类中有带参数的构造⽅法,重写Run ⽅法:输出Car is running。并重写ToString()⽅法:显⽰car中信息(显⽰车轮数、车重、车载⼈数)。最后编写主⽅法,定义car的对象,并调⽤Run⽅法,以及显⽰car中信息。
具体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Car car =new Car(4,10000.0,5);
car.Run();
car.ToString();
Console.Read();
}
}
class Vehicle
{
private int wheels;
private double weight;
public int Wheels
{
set{ wheels =value;}
get{return wheels;}
}
public double Weight
{
set{ weight =value;}
get{return weight;}
}
public Vehicle(int wheels,double weight)
{
this.wheels = wheels;
this.weight = weight;
}weigh weight 区别
public virtual void Run()
{
Console.WriteLine("running");
}
}
class Car : Vehicle
{
private int passenger_load;
public Car(int wheels,double 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()
{
Console.WriteLine("车轮数{0},车重{1},车载⼈数{2}", Wheels, Weight, passenger_load);
return base.ToString();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。