未使用的继承成员和方法

时间:2018-11-12 13:10:32

标签: c# inheritance

嗨,大家好:)我正在学习C#,希望获得有关继承成员/字段的帮助。我有一个叫做Car的类,假设只有一个引擎。但是,最近我想制造一款具有2个或更多引擎的赛车。从父类继承的字段“ my_Engine”似乎不合适,我将其保留为null。

问题1 Replace_Engine(Engine new_Engine)在Race_Car中不再可用

问题2 Replace_Engine(引擎目标,引擎new_Engine)只能在明确转换为Race_Car之后使用。

我有很多Car的子类。我该如何解决?我需要全部重写吗?

我应该全部重写它们,将my_Engine替换为数组是否足够好?但是并不是所有的汽车都有超过1个引擎...使用数组似乎很奇怪。我在这里很困惑。

感谢您的帮助:)

public class Car 
{
    //can store only one engine
    protected Engine my_Engine;

    //Changed to virtual to let Race_Car to override
    public virtual float Get_Horse_Power()
    {
        return my_Engine.max_Power;
    }

    //Changed to virtual to let Race_Car to override
    public virtual void Replace_Engine(Engine new_Engine)
    {
        my_Engine = new_Engine;
    }
}

public class Race_Car : Car
{
    protected Engine[] my_Engines;

    public override float Get_Horse_Power()
    {
        float result=0;
        for(int i = 0; i <my_Engines.Length;i++)
        {
            result+=my_Engines.max_Power;
        }
        return result;
    }

    //Don't know which engine need to be replaced
    public override void Replace_Engine(Engine new_Engine)
    {
        throw new System.NotImplementedException();
    }

    //Created a overloading method but need an explicit cast to call
    public void Replace_Engine(Engine target, Engine new_Engine)
    {
        for(int i = 0; i <my_Engines.Length;i++)
        {
            if(my_Engines[i]==target)
            {
                my_Engines[i]= new_Engine;
                return;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这里的基类假设在API中只有一个引擎,所以是的:对于不遵循该API的类型,这会很尴尬。您也许可以做类似的事情:

public virtual int EngineCount => 1;
public virtual void ReplaceEngine(Engine newEngine, int engineIndex = 0)
{
    if(engineIndex != 0) throw new ArgumentOutOfRangeException(nameof(engineIndex));
    _engine = newEngine;
}

和:

public override int EngineCount => 2;
public void override ReplaceEngine(Engine newEngine, int engineIndex = 0)
{
    switch(engineIndex)
    {
        case 1: _secondEngine = newEngine; break;
        default: base.ReplaceEngine(newEngine, engineIndex); break;
    }
}

答案 1 :(得分:0)

解决此问题的一种可能方法是将Engine的概念抽象化,并让您可能拥有多个物理单元来提供电源的事实与原始设计无关。

从代表引擎的抽象类开始:

public abstract class Engine
{
    public Engine()
    {

    }
    internal abstract float max_Power { get; }
}

然后实现两种引擎。一个简单的:

public class SimpleEngine : Engine
{
    private float _maxPower;

    public SimpleEngine(float maxPower)
    {
        _maxPower = maxPower;
    }


    internal override float MaxPower
    {
        get { return _maxPower; }            
    }
}

还有一个复合物:

public class CompositeEngine : Engine
{
    private List<Engine> _engines = new List<Engine>();

    public CompositeEngine(params Engine[] engines)
    {
        _engines.AddRange(engines);
    }

    internal override float MaxPower
    {
        get { return _engines.Sum(e => e.MaxPower); }             
    }
}

用法:

var engine = new CompositeEngine(new SimpleEngine(5), new SimpleEngine(4), new SimpleEngine(3));
var car = new Car();
car.Replace_Engine(engine);            
var value = car.Get_Horse_Power();

输出:

  

值= 12