C#控制台程序:请定义一个交通工具Vehicle类,其中包含的属性有速度(Speed)(只读)、体积(Size)(可读写)等;方法有Move()、设置速度SetSpeed(int speed)、加速SpeedUp(),减速SpeedDown()等。最后在Main()方法中实例化一个交通工具对象,初始化Size的值,通过方法给它的初始化Speed,并且通过输出方法打印出来。另外,调用加速、减速的方法对速度进行改变。
运行结果

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

namespace text7
{
class Program
{
static void Main(string[] args)
{
int a1 = 10;
int a2 = 20;
int change = 10;
Vehicle a = new Vehicle(a1);
a.SetSpeed(a2);
Console.WriteLine("size={0} speed={1}", a.Size, a.Speed);
a.SpeedUp(change);
Console.WriteLine("加速后:size={0} speed={1}", a.Size, a.Speed);
a.SpeedDown(change);
Console.WriteLine("减速后:size={0} speed={1}", a.Size, a.Speed);
Console.ReadLine();
}
}
class Vehicle
{
private int speed;
private int size;
public Vehicle(int a)
{
size = a;
}
public int Speed
{
get
{
return speed;
}

}
public int Size
{
get
{
return size;
}
set
{
size = value;
}
}
public void Move()
{

}
public bool SetSpeed(int a)
{
speed = a;
return true;
}
public void SpeedUp(int a)
{
speed += a;
}
public void SpeedDown(int a)
{
speed -= a;
}

}
}