0

我在让这个赛车程序运行时遇到了各种各样的困难。我有一个带有抽象方法的抽象赛车类,然后是从它派生的两个其他类。

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

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

然后是 = 符号的错误,表示类、结构或接口成员声明中的
标记无效'='

新的 Racer 必须返回一个类型

主要课程是

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;
        }
    }
}
4

2 回答 2

0

原始答案

我想你的意思是

    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]);
    }

}
于 2013-08-27T03:43:40.393 回答
0

派生类与其基类具有“is-a”关系。因此,在您的情况下, aHotRod是 aRacer并且 aStreetTuner是 a Racer,因此,当您将数组声明为 typeRacer时,任何 aRacer都可以放入该数组中,因此:

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;
}

HotRod构造函数也是如此StreetTuner

于 2013-08-27T03:49:10.690 回答