如何在C#中为类创建简化赋值或默认属性

Jac*_*itt 6 c#

这是次要的,我知道,但是我要说我有一个班级角色和一个班级能力(主要是因为这就是我正在做的事情).Class Character有六种能力(典型的D&D ......).基本上:

public class Character
{
    public Character()
    {
        this.Str = new Ability("Strength", "Str");
        this.Dex = new Ability("Dexterity", "Dex");
        this.Con = new Ability("Constitution", "Con");
        this.Int = new Ability("Intelligence", "Int");
        this.Wis = new Ability("Wisdom", "Wis");
        this.Cha = new Ability("Charisma", "Cha");
    }

    #region Abilities
    public Ability Str { get; set; }
    public Ability Dex { get; set; }
    public Ability Con { get; set; }
    public Ability Int { get; set; }
    public Ability Wis { get; set; }
    public Ability Cha { get; set; }
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

public class Ability
{
    public Ability()
    {
        Score = 10;
    }
    public Ability(string Name, string Abbr)
        : this()
    {
        this.Name = Name;
        this.Abbr = Abbr;
    }

    public string Name { get; set; }
    public string Abbr { get; set; }
    public int Score { get; set; }
    public int Mod
    {
        get
        {
            return (Score - 10) / 2;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在未来的代码中实际使用这些能力属性时,我希望能够默认只得分,如下所示:

//Conan hits someone
int damage = RollDice("2d6") + Conan.Str;

//evil sorcerer attack drains strength
Conan.Str = 0;
Run Code Online (Sandbox Code Playgroud)

而不是:

//Conan hits someone
int damage = RollDie("2d6") + Conan.Str.Score;

//evil sorcerer attack drains strength
Conan.Str.Score = 0;
Run Code Online (Sandbox Code Playgroud)

现在,第一种情况可以通过隐式转换来处理:

public static implicit operator int(Ability a)
{
    return a.Score;
}
Run Code Online (Sandbox Code Playgroud)

可以有人帮我反过来吗?这样的隐式转换:

public static implicit operator Ability(int a)
{
    return new Ability(){ Score = a };
}
Run Code Online (Sandbox Code Playgroud)

将替换整个属性而不仅仅是属性的分数 - 而不是所需的结果......

Gia*_*971 3

首先,保留隐式转换:

public static implicit operator Ability(int a)
{
     return new Ability(){ Score = a };
}
Run Code Online (Sandbox Code Playgroud)

然后在你的角色类中:为str添加一个私有的Ability属性,并更改Str属性的getter和setter,如下所示:

    private Ability str;
    public Ability Str 
    {
        get
        {
            return this.str;
        }
        set
        {
            if (value.Name == "")
            {
                this.str.Score = value.Score;
            }
            else
            {
                this.str = value;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

就这样吧:)

您还可以使用:

                if(string.IsNullOrWhiteSpace(value.Name))
Run Code Online (Sandbox Code Playgroud)

代替

                if (value.Name == "")
Run Code Online (Sandbox Code Playgroud)

如果您要编译为.NET 4.0版本

编辑:我给了你一个完全符合你想要的解决方案,但是 ja72 写的也是一个关于运算符 + 和 - 的好建议;你可以将他的解决方案添加到我的解决方案中(或者将我的解决方案添加到他的解决方案中,无论如何),它会很好地工作。然后你就可以写:

        Character Jax = new Character(); // Str.Score = 10
        Character Conan = new Character(); // Str.Score = 10

        Jax.Str = 2000; // Str.Score = 2000;
        Conan.Str += 150; // Str.Score = 160
Run Code Online (Sandbox Code Playgroud)