2013-08-27 61 views
0

我很难让这个赛车程序工作。我有一个抽象的racer类,有一个抽象方法,然后有两个从它派生出来的类。一个基类和两个派生类的继承

我有一个数组在我的主程序类,但错误上来称index [0][1]

数组大小不能在一个变量声明(尝试用“新”的表达初始化)

然后指定用于=错误登录说类
无效令牌'=',结构或接口成员声明

的新的赛车必须返回类型

的主类是

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

抽象赛车类是

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

发布您的所有代码,包括整个'Racer'类的代码。什么是“HotRod”和“StreetTuner”,派生类型? –

回答

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

} 
+0

使其更短:'Racer [] myarray = new Racer [2] {new HotRod(),new StreetTuner()};' – Nick

+0

当我删除基类Racer时出现同样的错误 – Jess

+0

然后,您需要发布更多你的代码 - 我的假设是'HotRod'和'StreetTuner'来自'Racer'。如果是这种情况,那么你在其他地方有一个语法错误。 –

0

派生类与他们的基类有一个“是-α”关系。所以你的情况,一个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构造为好。

+0

你写错了你的构造函数,看到我更新的答案。 –