为什么我得到一个"不包含一个构造函数的参数0"错误? C#
在我的形式加载,我有这样的code:
On my form load, I have this code:
private void Form1_Load(object sender, EventArgs e)
{
CharityCyclists cyclist1 = new CharityCyclists();
CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500);
cyclist1.Type = "Novelty Charity Cyclist";
cyclist1.Number = 1;
cyclist1.Finished = "Not Finished";
cyclist1.Hours = 0;
cyclist1.Mins = 0;
cyclist1.Secs = 0;
cyclist1.Bicycle = "Tricycle";
cyclist1.Wheels = 3;
cyclist1.FundsRaised = 300;
}
不过,我得到一个错误说'CycleEvent.CharityCyclists'不包含一个构造函数的参数0,它说的错误与code的这部分做的:
However, I'm getting a error saying "'CycleEvent.CharityCyclists' does not contain a constructor that takes 0 arguments", it says the error is to do with this part of the code:
CharityCyclists cyclist1 = new CharityCyclists();
下面是我的CharityCyclists类:
Here is my CharityCyclists class:
class CharityCyclists : Cyclists
{
private string bicycle;
private int wheels;
private double fundsRaised;
public string Bicycle
{
get { return bicycle; }
set { bicycle = value; }
}
public int Wheels
{
get { return wheels; }
set { wheels = value; }
}
public double FundsRaised
{
get { return fundsRaised; }
set { fundsRaised = 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, fundsRaised)
{
this.bicycle = bicycle;
this.wheels = wheels;
this.FundsRaised = fundsRaised;
}
public override string ToString()
{
return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels" ;
}
}
谢谢!
这是因为 CharityCyclists
类没有一个构造函数没有参数。
That is because the CharityCyclists
class does not have a constructor that takes no arguments.
C#编译器将生成默认的构造函数为你的如果没有定义其他构造的。
如果你自己定义构造函数(你有),C#编译器不会生成一个默认的构造。
The C# compiler will generate the default constructor for you, if you define no other constructors. If you define a constructor yourself (as you have), the C# compiler will not generate a default constructor.
如果你想允许 CharityCyclists
不带参数的构造,这增加code到类:
If you want to allow CharityCyclists
to be constructed without parameters, add this code to the class:
public CharityCyclists()
{}