【发布时间】:2016-05-07 02:34:28
【问题描述】:
我有下面的代码,IVehicle 接口有TestDrive 方法。
有一个抽象类——Vehicles,它实现了IVehicle,并有一个属性TestDriveTime。
有许多子类,如Car、Scooter 等(大约有 25 个这样的子类)。它派生自Vehicle 并有自己的TestDrive() 实现。
请帮助我解决我的问题 -
- 如果我必须依次运行每个
TestDrive,我在Main()中的代码是否正确? - 如果所有子对象都使用相同的
TestDriveTime,我该如何设置?
.
public interface IVehicle
{
void TestDrive();
}
public abstract class Vehicle : IVehicle
{
public DateTime TestDriveTime { get; set; }
abstract public void TestDrive();
}
public class Car : Vehicle
{
public override void TestDrive()
{
// code for car testDrive
// uses TestDriveTime
}
}
public class Scooter : Vehicle
{
public override void TestDrive()
{
// code for car testDrive
// uses TestDriveTime same as car
}
}
static void Main(string[] args)
{
IVehicle vehicle = null;
// 1. need to set the TestDriveTime which can be used for vehicles of all types.
vehicle = new Car();
vehicle.TestDrive();
// 2. Need to run the TestDrive for all vehicles sequentially
vehicle = new Scooter();
vehicle.TestDrive();
}
【问题讨论】:
标签: c# abstract-class derived-class base-class