【问题标题】:Inheritance of a base class and 2 derived classes一个基类和两个派生类的继承
【发布时间】:2013-08-27 03:41:53
【问题描述】:

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

我在我的主程序类中有一个数组,但出现错误,说 index [0][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;
        }
    }
}

【问题讨论】:

  • 发布所有代码,包括整个Racer 类的代码。什么是HotRodStreetTuner,派生类型?

标签: c# inheritance


【解决方案1】:

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

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

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

您需要编写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 构造函数也是如此。

【讨论】:

  • 你写错了你的构造函数,看我更新的答案。
【解决方案2】:

原答案

我想你是说

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

更新答案

看到你的实际代码后,你的主程序需要一些工作。首先,代码需要在methods中,而不是直接在classes中。其次,您使用,就好像它们是变量。我怀疑你想要这样的东西:

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

}

【讨论】:

  • 为了缩短:Racer[] myarray = new Racer[2] { new HotRod(), new StreetTuner() };
  • 那么您需要发布更多代码 - 我的假设是 HotRodStreetTuner 派生自 Racer。如果是这种情况,那么您在其他地方遇到了语法错误。
  • 我有 HotRod 和 StreetTuner 作为派生类
  • 发布给出错误的exact代码以及Racer和派生类的定义;否则我们只是在浪费时间猜测问题所在。
  • 新的 Racer 必须返回一个类型是 Racer 的错误
猜你喜欢
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 2012-12-10
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多