繁体   English   中英

基类和2个派生类的继承

[英]Inheritance of a base class and 2 derived classes

我很难让这个赛车程序开始工作。 我有一个带有抽象方法的抽象赛车类,然后是从该类派生的其他两个类。

我在主程序类中有一个数组,但是出现错误,指出index [0][1]

不能在变量声明中指定数组大小(尝试使用“ new”表达式初始化)

然后是一个错误,=符号说
类,结构或接口成员声明中的无效令牌'='

新赛车手必须返回一个类型

主要的类是

public class Program
{
    ApplicationUtilities.DisplayApplicationInformation();
    ApplicationUtilities.DisplayDivider("Start Racer Program");
    ApplicationUtilities.DisplayDivider("Prompt for Racer information and create first Racer");

        Racer[] myarray = new Racer[2];
        myarray[0] = new Racer(HotRod);
        myarray[1] = new Racer(StreetTuner);

    public CollectRacerInformation(HotRod);
}

抽象的Racer类是

public abstract class Racer
{
    private string racerName;
    private int racerSpeed;
    private Engine engine;

    public Racer();

    public Racer(string name, int speed, Engine engine);


    Engine engine();
    public abstract bool IsDead();
}

我派生的HotRod类是

public class HotRod : Racer
{
    private bool blower = true || false;

    public HotRod();

    public HotRod(string name, int speed, Engine engine);


    public override bool IsDead()
    {
        Engine engine1 = new Engine();
        engine1 = Engine1;
        Random rnd = new Random();
        rnd.NextDouble();
        bool dead = false;

        if (racerSpeed > 50 && rnd.NextDouble() > 0.6)
            if (engine1.horsePower < 300 && blower == true)
                dead = false;
            else
                dead = true;

        else if (racerSpeed > 100 && rnd.NextDouble() > 0.4)

            if (engine1.horsePower >= 300 && blower == true)
                dead = true;
            else
                dead = false;
        else
            dead = false;

        return dead;
    }

    public override string ToString()
    {
        string output;

        output = "\n============ HotRod Information ============";
        output += "\n\t              Racer's Name:\t" + racerName;
        output += "\n\t               Car's Speed:\t" + carSpeed;
        output += "\n\t          Engine Cylinders:\t" + engineCylinders;
        output += "\n\t         Engine Horsepower:\t" + engineHorsePower;
        output += "n\t               Racer's Type:\t" + racerType;
        output += "n\t          Racer with Blower:\t" + carBlower;
        output += "n\t             Still Working?:\t" + IsDead;

        return output;
    }
}

然后我派生的StreetTuner类是

public class StreetTuner : Racer
{
    private bool nitrous;

    public StreetTuner();

    public StreetTuner(string name, int speed, Engine engine);

    public bool IsDead
    {
        get { return IsDead; }
        set
        {
            if (speed > 50 && rnd.NextDouble() > 0.6)
                if (horsePower < 300 && blower == true)
                    IsDead = false;
                else
                    IsDead = true;
            else if (speed > 100 && rnd.NextDouble() > 0.4)
                if (horsePower >= 300 && blower == true)
                    IsDead = true;
                else
                    IsDead = false;
        }
    }
}

原始答案

我想你是说

    Racer[] myarray = new Racer[2];
    myarray[0] = new HotRod();
    myarray[1] = new StreetTuner();

更新的答案

在看到实际代码之后,您的主程序需要做一些工作。 首先,代码需要在方法中 ,也不能直接在类中 其次,您将当作变量使用 我怀疑您想要这样的东西:

public class Program
{
    public static void Main()
    {
        ApplicationUtilities.DisplayApplicationInformation();
        ApplicationUtilities.DisplayDivider("Start Racer Program");
        ApplicationUtilities.DisplayDivider("Prompt for Racer information and create first Racer");

        Racer[] myarray = new Racer[2];
        myarray[0] = new HotRod();
        myarray[1] = new StreetTuner();

        CollectRacerInformation(myarray[0]);
    }

}

派生类与其基类具有“ is-a”关系。 因此,在您的情况下, HotRodRacerStreetTunerRacer ,因此,当您将数组声明为Racer类型时,任何可以作为Racer东西都可以放入该数组中,因此:

var myarray = new Racer[2];
myarray[0] = new HotRod();
myarray[1] = new StreetTuner();

您可以显式实例化派生的类型,但是通过继承,可以在指定其基本类型的任何地方使用它们,因为它们是该类型( Racer基类)以及它们的具体类型(视具体情况而定,是HotRodStreetTuner )。

您需要编写Racer构造函数,如下所示:

// Default constructor
public Racer()
{

}

// Named value constructor
public Racer(string _name, int _speed, Engine _engine)
{
    // Store the name, speed and engine values
    racerName = _name; 
    racerSpeed = _speed; 
    engine = _engine;
}

HotRodStreetTuner构造函数也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM