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

标签 c# class oop constructor xna

这是我在其中构建构造函数的类,我想要多个“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/

相关文章:

php - PHP 中的 LXML 库?

php - 未找到“Michelf\Markdown”类

c++ - 对象的大小与没有指针的类的大小不同

c# - 在泛型方法中比较结构

c# - 尝试使用 Entity Framework Code First 更新数据库

c# - 抛出异常后我必须中断吗?

python - 是否可以在 Pyspark 中继承 DataFrame?

android - Android 中代码的可重用性

C++ 初学者 : expected unqualified-id before 'int'

c# - Blazor RenderFragment 到字符串