gpt4 book ai didi

c# - 在具有多个构造函数的单个类中使用对象

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:19 25 4
gpt4 key购买 nike

这是我在其中构建构造函数的类,我想要多个“UserKeys”构造函数,我的项目中的每个字符都有一个,但是主类只识别第一个构造函数(如果我有一个接受 0 个参数的构造函数,它识别第一个参数,但不识别第二个参数)

abstract class BaseKeys
{
public abstract bool LeftPressed();
public abstract bool RightPressed();
public abstract bool UpPressed();
public abstract bool DownPressed();
public abstract bool high_hitPressed();
public abstract bool rope_jumpPressed();
public abstract bool runRightPressed();
public override bool leftRightPressed();
}

class UserKeys : BaseKeys
{
#region data
Keys left, right, up, down, walk;
Keys combo, high_hit;
#endregion

#region ctor
public UserKeys(Keys left, Keys right,
Keys up, Keys down, Keys high_hit)
{
this.left = left;
this.right = right;
this.up = up;
this.down = down;
this.high_hit = high_hit;
}

public UserKeys(Keys right, Keys left,
Keys down, Keys walk, Keys high_hit)
{
this.left = left;
this.right = right;
this.down = down;
this.high_hit = high_hit;
this.walk = walk;
}
}

最佳答案

两个构造函数具有相同的参数,这是行不通的。只有他们的名字不同。您需要让它们与众不同:

public UserKeys(Keys left, Keys right,
Keys up, Keys down,
Keys high_hit)
{
this.left = left;
this.right = right;
this.up = up;
this.down = down;
this.high_hit = high_hit;
}

public UserKeys(Keys left, Keys right,
Keys up, Keys down,
Keys high_hit, Keys walk)
{
this.left = left;
this.right = right;
this.up = up;
this.down = down;
this.high_hit = high_hit;
this.walk = walk;
}

我已将 Keys up 添加到第二个,并使用与第一个相同的顺序(否则非常困惑且容易出错)。如果您不知道 Keys up,请传递 Keys.None

关于c# - 在具有多个构造函数的单个类中使用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com