参数未传递给主类

时间:2012-11-08 20:59:03

标签: c# parameters constructor

我目前遇到的问题是我的程序应该将每个类的参数显示在表单上,​​但是层次结构中最低的类没有显示。例如;我希望它能够展示“慈善自行车骑行者1号骑3轮三轮车和300英镑的赞助”,但它只展示了“慈善自行车骑行者1号骑3轮三轮车”。

这是我的赞助课程:

class Sponsorship : CharityCyclists
{
    private double fundsRaised;

    public double FundsRaised
    {
        get { return fundsRaised; }
        set { fundsRaised = value; }
    }

    public Sponsorship(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, bicycle, wheels, fundsRaised)
    {
        this.fundsRaised = fundsRaised;
    }

    public override string ToString()
    {
        return base.ToString() + " and sponsorship amount: " + fundsRaised;
    }
}

这是我的CharityCyclists类:

class CharityCyclists : Cyclists
{

    private string bicycle;
    private int wheels;

    public string Bicycle
    {
        get { return bicycle; }
        set { bicycle = value; }
    }

    public int Wheels
    {
        get { return wheels; }
        set { wheels = value; }
    }


    public CharityCyclists(string type, int number, string finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs)
    {
        this.bicycle = bicycle;
        this.wheels = wheels;
    }

    public override string ToString()
    {
        return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels";
    }
}

和我的自行车运动员课程:

    public static int NumFinished
    {
        get { return Cyclists.numFinished; }
        set { Cyclists.numFinished = value; }
    }

    public string Type
    {
        get { return type; }
        set { type = value; }
    }

    public int Number
    {
        get { return number; }
        set { number = value; }
    }

    public string Finished
    {
        get { return finished; }
        set { finished = value; }
    }

    public int Hours
    {
        get { return hours; }
        set { hours = value; }
    }

    public int Mins
    {
        get { return mins; }
        set { mins = value; }
    }

    public int Secs
    {
        get { return secs; }
        set { secs = value; }
    }

    public Cyclists(string type, int number, string finished, int hours, int mins, int secs)
    {
        this.type = type;
        this.number = number;
        this.finished = finished;
        this.hours = hours;
        this.mins = mins;
        this.secs = secs;
    }

    public override string ToString()
    {

        return type + " No. " + number;
    }

    public Cyclists()
    {

    }

这是将它们添加到表单上的列表框中的位置:

        Cyclists[] cyclistsList;
        cyclistsList = new Cyclists[2];

        cyclistsList[0] = new CharityCyclists("Novelty Charity Cyclist", 1, "Not Finished", 0, 0, 0, "Tricycle", 2, 300);
        cyclistsList[1] = new CompetitiveCyclists("Competitive Cyclist", 1, "Not Finished", 0, 0, 0, 25, 2);

        for (int i = 0; i < MAX_CYCLISTS; i++)
        {
            lstCyclists.Items.Add(cyclistsList[i]);
        }

谢谢!

1 个答案:

答案 0 :(得分:2)

您根本没有创建赞助实例。此时您正在调用CharSc(C)的类,而不是Sponsorship,因为这个构造函数调用:cyclistsList[0] = new CharityCyclists("Novelty Charity Cyclist", 1, "Not Finished", 0, 0, 0, "Tricycle", 2, 300);您应该像这样修改代码:

Cyclists[] cyclistsList;
        cyclistsList = new Cyclists[2];

    cyclistsList[0] = new Sponsorship("Novelty Charity Cyclist", 1, "Not Finished", 0, 0, 0, "Tricycle", 2, 300);
    cyclistsList[1] = new CompetitiveCyclists("Competitive Cyclist", 1, "Not Finished", 0, 0, 0, 25, 2);

    for (int i = 0; i < MAX_CYCLISTS; i++)
    {
        lstCyclists.Items.Add(cyclistsList[i]);
    }

关于多态,你需要创建一个类的实例来覆盖你想要调用的方法。