C# 尝试创建多值列表会导致错误
我试图制作一个列表,其中包含Program.cs和Game.cs类中的两个参数。在Program.cs中,我这样做了:(错误是在game下给出的,它说:game不包含一个接受2个参数的构造函数。C# 尝试创建多值列表会导致错误,c#,C#,我试图制作一个列表,其中包含Program.cs和Game.cs类中的两个参数。在Program.cs中,我这样做了:(错误是在game下给出的,它说:game不包含一个接受2个参数的构造函数。 List JoinLists=新列表(); 加入列表。添加(新游戏(“Elf”,6)); In-Game.cs我写了以下内容: public class Game { private string _type; private int _strength; public voi
List JoinLists=新列表();
加入列表。添加(新游戏(“Elf”,6));
In-Game.cs我写了以下内容:
public class Game
{
private string _type;
private int _strength;
public void JoinLists(string charType, int strength)
{
this._type = charType;
this._strength = strength;
}
}
你的错误是不言自明的
游戏没有包含一个带2个参数的构造函数,这意味着你应该添加带2个参数的构造函数,很明显
公共类游戏
{
私有字符串_类型;
私人综合实力;
公共游戏(字符串图表类型,整数强度)
{
这个._type=charType;
这个。_力量=力量;
}
}
从
构造函数是一种方法,其名称与它的
其方法签名仅包括方法名称及其
参数列表;它不包括返回类型
您也可以省略这个
指针,因为您的类字段和构造函数参数的名称不同,所以不需要使用这个
来限定它们。您的错误是非常自明的游戏不包含带2个参数的构造函数
,这意味着您应该使用显然是两个论点
公共类游戏
{
私有字符串_类型;
私人综合实力;
公共游戏(字符串图表类型,整数强度)
{
这个._type=charType;
这个。_力量=力量;
}
}
从
构造函数是一种方法,其名称与它的
其方法签名仅包括方法名称及其
参数列表;它不包括返回类型
您还可以省略此指针,因为类字段和构造函数参数的名称不同,因此无需使用此限定它们,构造函数必须与类具有相同的名称:
public class Game
{
private string _type;
private int _strength;
public Game(string charType, int strength)
{
this._type = charType;
this._strength = strength;
}
}
签名中没有返回
请阅读此处的详细信息:构造函数必须与类具有相同的名称:
public class Game
{
private string _type;
private int _strength;
public Game(string charType, int strength)
{
this._type = charType;
this._strength = strength;
}
}
签名中没有返回
在此处阅读更多信息:创建构造函数
public Game(string charType, int strength)
{
// code here
}
或者您可以更改创建游戏
List<Game> JoinLists = new List<Game>();
JoinLists.Add(new Game().JoinLists("Elf", 6));
List JoinLists=新列表();
添加(新游戏().joinlist(“Elf”,6));
创建一个构造函数
public Game(string charType, int strength)
{
// code here
}
或者您可以更改创建游戏
List<Game> JoinLists = new List<Game>();
JoinLists.Add(new Game().JoinLists("Elf", 6));
List JoinLists=新列表();
添加(新游戏().joinlist(“Elf”,6));
构造函数必须与所驻留的类同名,将公共无效JoinList(string ChartType,int strength)
更改为公共游戏(string ChartType,int strength)
,您应该可以。更多信息构造函数必须与所驻留的类同名,更改公共无效JoinList(字符串图表类型,int-strength)
到公共游戏(字符串图表类型,int-strength)
你应该没事。更多信息