在基类中使用派生类方法

时间:2018-05-02 15:55:48

标签: c# class oop inheritance polymorphism

在我的工作中我试图写一个车库系统管理。 这是一个抽象类车辆(它有轮子列表(也是一个类),一个引擎,以及更多无关紧要的参数), 汽车是由vihecle,(也是摩托车)派生的 和汽车上的燃料工作,汽车上的电力来自汽车。(与摩托车相同)。

每辆车都有其他引擎,因此我在构造函数中使用 i_Engine(在Engine内)= new WorkedOnFuel(WorkedOnFuel.eFuelType.Soler,0,115);

每个WorkedOnFuel / WorkedOnElectricity类都有自己的方法来为自己的发动机增加能量(燃料需要燃料类型和升数,电力需要浮动小时才能增加)。

是否有可能投射和使用这种方法?

注意:我不能使用Interface!

我在这里按类添加代码:

'default' => 0

}

public abstract class Vehicle
{


    private string m_ModelName;
    private string m_LicenseNumber;
    private List<Wheel> m_Wheels;
    private Engine m_Engine;

    public Vehicle(string i_ModelName, string i_LicenseNumber)
    {
        m_ModelName = i_ModelName;
        m_LicenseNumber = i_LicenseNumber;
        m_Wheels = new List<Wheel>();
    }

    public Engine i_Engine
    {
        get
        {
            return m_Engine;
        }
        set
        {
            m_Engine = value;
        }
    }

    public List<Wheel> i_Wheels
    {
        get
        {
            return m_Wheels;
        }
        set
        {
            m_Wheels = value;
        }
    }

    public string i_ModelName
    {
        get
        {
            return m_ModelName;
        }
        set
        {
            m_ModelName = value;
        }
    }

    public string i_LicenseNumber
    {
        get
        {
            return m_LicenseNumber;
        }
        set
        {
            m_LicenseNumber = value;
        }
    }
}

}

public abstract class Engine
{
    private float m_Current;
    private float m_Max;
    private float m_PercentageOfEnergyLeft;

    public Engine(float i_Current, float i_Max)
    {
        m_Current = i_Current;
        m_Max = i_Max;
        m_PercentageOfEnergyLeft = 0;
    }

    public float i_Current
    {
        get
        {
            return m_Current;
        }
        set
        {
            m_Current = value;
        }
    }

    public float i_Max
    {
        get
        {
            return m_Max;
        }
        set
        {
            m_Max = value;
        }
    }

    public float i_PercentageOfEnergyLeft
    {
        get
        {
            return m_PercentageOfEnergyLeft;
        }
        set
        {
            m_PercentageOfEnergyLeft = value;
        }
    }



}

}

public class WorkedOnFuel : Engine
{
    public enum eFuelType
    {
        Soler,
        Octan95,
        Octan96,
        Octan98
    }

    private float m_CurrentFuelAmount;
    private float m_MaxFuelAmount;
    private eFuelType m_FuelType;

    public WorkedOnFuel(eFuelType i_Fueltype, float i_CurrentFuelAmount, float i_MaxFuelAmount) : base(i_CurrentFuelAmount,i_MaxFuelAmount)
    {
        m_FuelType = i_Fueltype;
        m_CurrentFuelAmount = i_CurrentFuelAmount;
        m_MaxFuelAmount = i_MaxFuelAmount;
    }

    public eFuelType i_Fueltype
    {
        get
        {
            return m_FuelType;
        }
        set
        {
            m_FuelType = value;
        }
    }

    public float i_CurrentFuelAmount
    {
        get
        {
            return m_CurrentFuelAmount;
        }
        set
        {
            m_CurrentFuelAmount = value ;
        }
    }

    public float i_MaxFuelAmount
    {
        get
        {
            return m_MaxFuelAmount;
        }
        set
        {
            m_MaxFuelAmount = value;
        }
    }
    public virtual void AddEnergyToEngine(eFuelType i_Type, float i_FuelAmountForAdd)
    {
        if (i_Type == i_Fueltype && i_CurrentFuelAmount + i_FuelAmountForAdd <= i_MaxFuelAmount)

        {
            i_CurrentFuelAmount += i_FuelAmountForAdd;
            i_PercentageOfEnergyLeft = i_CurrentFuelAmount / i_MaxFuelAmount;
        }
    }
}

}

我需要的结果是在主程序中生病时 车辆v =新FuelCar(...) v.i_Engine .---- 通过汽车类型的发动机获得电力/燃料的方法。(它是在构造函数中构建的。

燃料汽车类:

public class WorkedOnElectricity : Engine
{
    private float m_BatteryTime;
    private float m_MaxBatteryTime;

    public WorkedOnElectricity(float i_BatteryTime, float i_MaxBatteryTime): base(i_BatteryTime,i_MaxBatteryTime)
    {
        m_BatteryTime = i_BatteryTime;
        m_MaxBatteryTime = i_MaxBatteryTime;

    }

    public float i_BatteryTime
    {
        get
        {
            return m_BatteryTime;
        }
        set
        {
            m_BatteryTime = value;
        }
    }

    public float i_MaxBatteryTime
    {
        get
        {
            return m_MaxBatteryTime;
        }
        set
        {
            m_MaxBatteryTime = value;
        }
    }

    public virtual void AddEnergyToEngine(float i_HoursToAddTobattery)
    {
        if ((m_BatteryTime + i_HoursToAddTobattery) <= m_MaxBatteryTime)
        {
            i_MaxBatteryTime += i_HoursToAddTobattery;
            i_PercentageOfEnergyLeft = i_BatteryTime / i_MaxBatteryTime;
        }
    }


}

}

非常感谢您的帮助,对不起我的英语和指定问题! 本。

1 个答案:

答案 0 :(得分:1)

我相信你所要求的是“沮丧”。在我看来,C#具有优雅地处理此操作的语句。 “as”运算符可用于非常容易地确定对象的类型。另外,在Type类上有一个名为IsInstanceOfType的方法。

“as”运算符的文档: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as

“IsInstanceOfType”方法的文档: https://msdn.microsoft.com/en-us/library/system.type.isinstanceoftype%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

使用“as”运算符来确定派生类的类型,从而使您能够调用派生类的方法,如下所示:

Vehicle vehicle1 = new FuelCar(...);
Vehicle vehicle2 = new ElectricCar(...);
.
.
.
FuelCar fuelCar = vehicle1 as FuelCar;
if (fuelCar != null)
{
    fuelCar.CallAFuelCarMethod();
}

ElectricCar electricCar = vehicle2 as ElectricCar;
if (electricCar != null)
{
    electricCar.CallAnElectricCarMethod();
}
.
.
.

使用IsInstanceOfType方法类似:

if (vehicle.GetType().IsInstanceOfType(typeof(FuelCar))
{
    .
    .
    .
}

可能还有其他方法;然而,这两个是简单的解决方案。