假设我有以下代码:
public abstract class Vehicle
{
public Transmission MyTransmission;
public int NumberOfWheels;
public int NumberOfSeats;
public int Weight;
public int TopSpeed; //etc.
}
public class Car : Vehicle
{
public int SteeringWheelSize = 10;
}
public class Truck : Vehicle
{
public int SteeringWheelSize = 20;
}
public class Bike : Vehicle
{
}
public class FourWheelTransmission
{
public Vehicle ParentVehicle;
public Transmission(Vehicle vehicle)
{
ParentVehicle = vehicle;
//here, I want to access the SteeringWheelSize from its ParentVehicle object.
}
}
要完成最后评论中的目标,最好的方法应该是什么?当然,一种方法是创建两个抽象类:“公共抽象类 TwoWheelVehicle”和“公共抽象类 FourWheelVehicle”,但是如果我们不想重复 FourwheelVehicle 和 TwoWheelVehicle 中常见的所有相似属性怎么办?允许 FourWheelTransmission 类访问其车辆的 SteeringWheelSize 的其他方式?