C# 关于枚举方法

C# 关于枚举方法,c#,visual-studio,C#,Visual Studio,大家好,我是C#的新手,我有一些问题。我目前正在学校做作业,我真的被困在这一部分。我盯着它,用谷歌搜索了好几个小时,我运气不错。我已经在下面列出了我到目前为止所做的 方向不太好 为4个映像状态创建公共枚举CharacterState:攻击、防御、空闲和死亡。 现在创建一个成员变量状态来保存字符状态和一个带有get的公共属性状态 现在,请填写get和set的默认行为,以返回/设置state的值。“ 任何帮助都将受到极大的欢迎!多谢各位 namespace WPFBattle { class

大家好,我是C#的新手,我有一些问题。我目前正在学校做作业,我真的被困在这一部分。我盯着它,用谷歌搜索了好几个小时,我运气不错。我已经在下面列出了我到目前为止所做的

方向不太好

为4个映像状态创建公共枚举CharacterState:攻击、防御、空闲和死亡。 现在创建一个成员变量状态来保存字符状态和一个带有get的公共属性状态 现在,请填写get和set的默认行为,以返回/设置state的值。“

任何帮助都将受到极大的欢迎!多谢各位

namespace WPFBattle
{
    class CharacterImage: System.Windows.Controls.Image
    {
        public enum Attacking{

        }

        public enum Defending{

        }

        public enum Idle{

        }

        public enum Dead{

        }
        public ImageSource IdleImageSource { get; set; }
        public ImageSource AttackingImageSource { get; set; }
        public ImageSource TakeDamageImageSource { get; set; }
        public ImageSource DeadImageSource { get; set; }

        protected void UpdateImageSource()
        {
            switch (State)
            {
                case CharacterState.Attacking:
                    this.Source = AttackingImageSource;
                    break;
                case CharacterState.TakeDamage:
                    this.Source = TakeDamageImageSource;
                    break;
                case CharacterState.Dead:
                    this.Source = DeadImageSource;
                    break;
                case CharacterState.Idle:
                default:
                    this.Source = IdleImageSource;
                    break;
            }
        }

        protected override void OnRender(DrawingContext dc)
        {
            UpdateImageSource();
            base.OnRender(dc);
        }

        public CharacterState State
        {
            get { return state; }
            set
            {
                state = value;
                this.Dispatcher.Invoke((Action)(() =>
                {
                    UpdateImageSource();
                }));
            }
        }
    }
}

您的枚举被声明为错误。这就是你想要的

enum CharacterState
{
    Attacking,
    Defending,
    Idle,
    Dead
}

class CharacterImage 
{

    private CharacterState state;
    public CharacterState State
    {
        get { return state; }
        set
        {
            if (state == value)
                return;

            state = value;               
        }
    }
}
您也可以将您的属性写为

public CharacterState State { get; set; }
这与上述属性编译的代码相同,除了值比较部分

if (state == value)
    return;

您的枚举被声明为错误。这就是你想要的

enum CharacterState
{
    Attacking,
    Defending,
    Idle,
    Dead
}

class CharacterImage 
{

    private CharacterState state;
    public CharacterState State
    {
        get { return state; }
        set
        {
            if (state == value)
                return;

            state = value;               
        }
    }
}
您也可以将您的属性写为

public CharacterState State { get; set; }
这与上述属性编译的代码相同,除了值比较部分

if (state == value)
    return;

从枚举开始

为4个映像状态创建公共枚举CharacterState:攻击、防御、空闲和死亡

从代码中,您已经为每个状态创建了一个枚举。枚举用于定义选项,在您的情况下,您应该有一个枚举来定义角色的状态

public enum CharacterState {
    Attacking,
    Defending,
    Idle,
    Dead
}
。。。现在创建一个成员变量状态,用get和set保存字符状态和公共属性状态

然后可以通过角色的状态属性访问角色的当前状态

var character = new Character();
CharacterState state = character.State; // returns Idle.
// or set it
character.State = CharacterState.Attacking;
Console.WriteLine($"Take cover! character is {character.State}");
//- logs "Take cover! character is Attacking"

希望这能澄清问题。

从您的枚举开始

为4个映像状态创建公共枚举CharacterState:攻击、防御、空闲和死亡

从代码中,您已经为每个状态创建了一个枚举。枚举用于定义选项,在您的情况下,您应该有一个枚举来定义角色的状态

public enum CharacterState {
    Attacking,
    Defending,
    Idle,
    Dead
}
。。。现在创建一个成员变量状态,用get和set保存字符状态和公共属性状态

然后可以通过角色的状态属性访问角色的当前状态

var character = new Character();
CharacterState state = character.State; // returns Idle.
// or set it
character.State = CharacterState.Attacking;
Console.WriteLine($"Take cover! character is {character.State}");
//- logs "Take cover! character is Attacking"

希望这能澄清问题。

首先,正如您所写的,声明中说:为4个映像状态创建一个公共枚举。这只是一个具有4个可能值的对象。创建,然后使用属性/字段成员管理内部值。可能是这样的:

    namespace WPFBattle
{
    public enum CharacterState{
        Attacking = 1,
        Defending = 2,
        Idle = 3,
        Dead
    }

    class CharacterImage: System.Windows.Controls.Image
    {

        public CharacterState _state;

        public ImageSource IdleImageSource { get; set; }
        public ImageSource AttackingImageSource { get; set; }
        public ImageSource TakeDamageImageSource { get; set; }
        public ImageSource DeadImageSource { get; set; }

        protected void UpdateImageSource()
        {
            switch (_state)
            {
                case CharacterState.Attacking:
                    this.Source = AttackingImageSource;
                    break;
                case CharacterState.TakeDamage:
                    this.Source = TakeDamageImageSource;
                    break;
                case CharacterState.Dead:
                    this.Source = DeadImageSource;
                    break;
                case CharacterState.Idle:
                default:
                    this.Source = IdleImageSource;
                    break;
            }
        }

        protected override void OnRender(DrawingContext dc)
        {
            UpdateImageSource();
            base.OnRender(dc);
        }

        public CharacterState State
        {
            get { return _state; }
            set
            {
                _state = value;
                this.Dispatcher.Invoke((Action)(() =>
                {
                    UpdateImageSource();
                }));
            }
        }
    }
}

首先,正如您所写的,声明中说:为4个映像状态创建一个公共枚举。这只是一个具有4个可能值的对象。创建,然后使用属性/字段成员管理内部值。可能是这样的:

    namespace WPFBattle
{
    public enum CharacterState{
        Attacking = 1,
        Defending = 2,
        Idle = 3,
        Dead
    }

    class CharacterImage: System.Windows.Controls.Image
    {

        public CharacterState _state;

        public ImageSource IdleImageSource { get; set; }
        public ImageSource AttackingImageSource { get; set; }
        public ImageSource TakeDamageImageSource { get; set; }
        public ImageSource DeadImageSource { get; set; }

        protected void UpdateImageSource()
        {
            switch (_state)
            {
                case CharacterState.Attacking:
                    this.Source = AttackingImageSource;
                    break;
                case CharacterState.TakeDamage:
                    this.Source = TakeDamageImageSource;
                    break;
                case CharacterState.Dead:
                    this.Source = DeadImageSource;
                    break;
                case CharacterState.Idle:
                default:
                    this.Source = IdleImageSource;
                    break;
            }
        }

        protected override void OnRender(DrawingContext dc)
        {
            UpdateImageSource();
            base.OnRender(dc);
        }

        public CharacterState State
        {
            get { return _state; }
            set
            {
                _state = value;
                this.Dispatcher.Invoke((Action)(() =>
                {
                    UpdateImageSource();
                }));
            }
        }
    }
}

好的,你有4个空枚举,而不是只有一个有4个值。你的代码中的CharacterState是什么?我仍然不确定你的问题到底是什么。哦,好的,我刚刚修复了枚举部分。它现在可以工作了,谢谢你能回答你的问题让其他人也能受益吗?好吧,你有4个空枚举,而不是只有一个有4个值。你的代码中的CharacterState是什么?我仍然不确定你的问题到底是什么。哦,好的,我刚刚修复了枚举部分。现在可以了,谢谢你能回答你的问题让其他人也能受益吗?非常感谢你的回答!我修复了enum部分,但是对于“现在创建一个成员变量状态来保存字符状态和一个带有get和set的公共属性状态”部分,我仍然有点困惑。我不确定如何解决这个问题。我用一些评论更新了答案。请注意,
state
是成员变量,只能从属性
state
更新,请注意不同的大小写和
private/public
修饰符<代码>状态只能从类内访问,而
状态
将其封装。这意味着您可以通过访问公共属性
state
来读写
state
。非常感谢您的回复!我修复了enum部分,但是对于“现在创建一个成员变量状态来保存字符状态和一个带有get和set的公共属性状态”部分,我仍然有点困惑。我不确定如何解决这个问题。我用一些评论更新了答案。请注意,
state
是成员变量,只能从属性
state
更新,请注意不同的大小写和
private/public
修饰符<代码>状态只能从类内访问,而
状态
将其封装。这意味着您可以通过访问公共属性
state
来读写
state