基类和2个派生类的继承

时间:2013-08-27 03:41:53

标签: c# inheritance

让这个赛车计划发挥作用我遇到了各种困难。我有一个抽象的racer类,它有一个抽象方法,然后是另外两个派生自它的类。

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

无法在变量声明中指定数组大小(尝试使用'new'表达式初始化)

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

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

2 个答案:

答案 0 :(得分: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]);
    }

}

答案 1 :(得分:0)

派生类与其基类有“is-a”关系。因此,在您的情况下,HotRodRacerStreetTunerRacer,因此当您声明数组属于Racer类型时,则任何Racer的内容都可以放入该数组中,因此:

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

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

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

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构造函数。