猿问

如何增加派生类的值

我正在建模一个模拟汽车和卡车的驾驶和加油的程序。这就是我到目前为止所做的:


public abstract class Vehicle

{

    protected Vehicle(double fuelQuantity, double fuelConsumption)

    {

        this.FuelQuantity = fuelQuantity;

        this.FuelConsumption = fuelConsumption;

    }


    public double FuelQuantity { get; protected set; } 


    public double FuelConsumption { get; protected set; } 


    public abstract string Drive(double distance);


    public abstract void Refuel(double liters);

}


public class Car : Vehicle

{

    public Car(double fuelQuantity, double fuelConsumption) : base      (fuelQuantity, fuelConsumption)

    {       

    }


    public override string Drive(double distance)

    {

    }


    public override void Refuel(double liters)

    {

    }

}

所以,我想增加0.9升汽车的油耗值(现在是夏天,所以汽车使用空调)。哪里可以做到这一点?我不想在构造函数中这样做,因为我认为这样不行。


qq_笑_17
浏览 104回答 3
3回答

慕码人2483693

这将是添加装饰器的好地方。一些伪代码(不完整!)但希望您能明白。public class VehicleDecorator : Vehicle  public VehicleDecorator(Vehicle vehicle)  {      this.vehicle = vehicle;  } public class VehicleWithAc : VehicleDecorator  public VehicleWithAc(Vehicle vehicle) : base(vehicle){}  public override double FuelConsumption {                get{                     return base.FuelConsumption+0.9 } }然后在您的程序中创建您的汽车并使用 VehicleWithAc 装饰器对其进行装饰Program   var baseCar = new Car();   var summerDriver = new VehicleWithAc(baseCar)

眼眸繁星

我认为你遇到的问题是你fuelConsumption作为单个变量传递给构造函数,从而说明这是汽车的油耗,完全停止。正如您所发现的,解决这个问题 - 燃油消耗不是一个静态的东西,它取决于其他变量,例如空调是否打开。Doug 已经接近提到装饰器了,但我认为它可以更简单一些,但更灵活。我认为您仍然应该传递一个燃油消耗数据,为了简单起见,我们将其称为baseFuelConsumption。请记住,车辆通常根据城市和高速公路的燃油消耗进行分级,因为它们通常不同,但出于此目的,我们将忽略它。抛开行驶距离等,我们有:public abstract class Vehicle{    private readonly double _baseFuelConsumption;    protected double BaseFuelConsumption => _baseFuelConsumption;    protected Vehicle(double baseFuelConsumption) => _baseFuelConsumption = baseFuelConsumption;    public virtual double ActualFuelConsumption => BaseFuelConsumption;}那么,空调会额外消耗多少油耗呢?让我们以 Doug 的答案为基点,并将其赋予我们的汽车......public class Car : Vehicle{    private const double _ACModifier = 0.9;    public Car()    :base(1)    {           }    public bool IsACOn { get; set; }    public override double ActualFuelConsumption    {        get        {            double consumption = base.ActualFuelConsumption;            consumption += IsACOn ? _ACModifier : 0;            return consumption;        }    }}现在,为了进行模拟,您可以随着时间的推移打开和关闭交流电,并ActualFuelConsumption随着时间的推移测量属性。

慕田峪9158850

如果您想使用此结构,您必须为属性设置自定义规则。 public abstract class Vehicle{    protected Vehicle(double fuelQuantity, double fuelConsumption)    {        this.FuelQuantity = fuelQuantity;        this._fuelConsumption = fuelConsumption;        this.FuelConsumption = fuelConsumption;    }    public double FuelQuantity { get; protected set; }    private double _fuelConsumption { get;  set; }    public double FuelConsumption {        get { return _fuelConsumption; }        protected set {            _fuelConsumption = (_fuelConsumption + 0.9);        } }    public abstract string Drive(double distance);    public abstract void Refuel(double liters);}因为您的类是抽象的,并且派生类中的属性没有可重写的属性,所以您无法访问基属性。例如,您可以在集合中使用条件public double FuelConsumption {    get { return _fuelConsumption; }    protected set {      if(Issummer)      {      _fuelConsumption = (_fuelConsumption + 0.9);      }else{ _fuelConsumption =_fuelConsumption;}    } }
随时随地看视频慕课网APP
我要回答