C#控制台程序:定义一个名为Vehicles(交通工具)的基类,并添加Brand(品牌)和Color(颜色),以及成员方法Run(行驶,在控制台显示“我已经开动了”),ShowInfo(显示品牌和颜色),添加构造方法对成员属性初始化。编写Car类继承于Vehicles类,增加int型成员属性Seats(座位)和成员方法ShowInfo(在控制台显示小汽车的信息),并编写构造方法对其初始化。编写Truck(卡车)类继承于Vehicles类,增加float型成员属性Load(载重)和成员方法ShowInfo(在控制台显示卡车的信息),并编写构造方法。
运行结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
class Vehicles
{
public string Brand;
public string Color;
public Vehicles(string inBrand, string inColor)
{
Brand=inBrand;
Color=inColor;
}
public Vehicles()
{
}
public void Run()
{
Console.WriteLine("我已经开动了");
}
public virtual void ShowInfo()
{
Console.WriteLine("商标:{0},颜色:{1}",Brand,Color);
}
}

class Car : Vehicles
{
public int Seats;
public Car(int inSeats)
{
Seats=inSeats;
}
public override void ShowInfo()
{
Console.WriteLine("座位:{0}", Seats);
}
}

class Truck : Vehicles
{
public float Load;
public Truck(float inLoad)
{
Load = inLoad;
}
public override void ShowInfo()
{
Console.WriteLine("载重:{0}吨", Load);
}
}

class program
{
static void Main()
{
Vehicles s1 = new Vehicles("奔驰", "红色");
Vehicles s4 = new Vehicles("东风", "蓝色");
Car s2 = new Car(4);
Truck s3 = new Truck(2);
s1.ShowInfo();
s2.ShowInfo();
s4.ShowInfo();
s3.ShowInfo();
Console.ReadLine();
}
}

}

Linches.cn | 林澈思的茶 微信小程序