我有一些类,其中包含多个属性,它们具有明确定义的名称和功能,但具有相同的实现。例如:
class Stats
{
private int attack;
public int Attack
{
get =>
HasBuff ? attack + 1 : attack;
set
{
if (value < 1 || value > 10)
throw new ArgumentOutOfRangeException("Invalid value");
attack = value;
}
}
public int Defense {...}
public int Speed {...}
}
如何将防御和速度的实现方式与攻击相似,以避免冗余并使更改变得更加容易?
PropertyBag<int>
,但除非你有很多属性,否则它需要的工作量可能比它值得的要多。 - Mark Benningfield