我在让这个赛车程序运行时遇到了各种各样的困难。我有一个带有抽象方法的抽象赛车类,然后是从它派生的两个其他类。
我在我的主程序类中有一个数组,但出现错误说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;
}
}
}