改进13参数构造函数 [英] Improve on a 13-parameter constructor

查看:71
本文介绍了改进13参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢Microsoft的Intellisense和Atomineer的Atomineer Utils ...所有这些参数都是必填且不可变的.

Thank Microsoft for Intellisense and Atomineer for Atomineer Utils...All of these parameters are required and immutable.

有更好的方法吗?

/**************************************************************************************************
 * <summary>Initializes a new instance of the ADTBattleCharacter class.</summary>
 * <param name="name">         The name of the character.</param>
 * <param name="max_HP">       The maximum hit points.</param>
 * <param name="max_MP">       The maximum magic power.</param>
 * <param name="strength">     The strength.</param>
 * <param name="agility">      The agility.</param>
 * <param name="attack_power"> The attack power.</param>
 * <param name="defense_power">The defense power.</param>
 * <param name="gold">         The gold carried by the character.</param>
 * <param name="experience">   The experience the character is worth.</param>
 * <param name="stop_resist">  The character's resistance to stopspell.</param>
 * <param name="sleep_resist"> The character's resistance to sleep.</param>
 * <param name="hurt_resist">  The character's resistance to hurt/hurtmore.</param>
 * <param name="spell_list">   Available spells.</param>
 **************************************************************************************************/
ADTBattleCharacter(std::string name, unsigned char max_HP, unsigned char max_MP,
                   unsigned char strength, unsigned char agility,
                   unsigned char attack_power, unsigned char defense_power,
                   unsigned short gold, unsigned short experience,
                   double stop_resist, double sleep_resist, double hurt_resist,
                   std::bitset<SPELL_MAX> spell_list);

推荐答案

看您的具体情况,在我看来您还没有很好地解决问题.

Looking at your specific case, it seems to me that you haven't broken things out very well.

从概念上讲,系统中的字符具有:

Conceptually, a character in your system has:

  • 一个名字.
  • 一个统计块,包含其基本统计信息(HP,防御等).
  • 角色的次要属性(体验).
  • 库存,其中包括其当前的咒语列表和金币,以及其他可能的信息.

这是4个参数,而不是13个.在编写函数时,您会看到它使用了大量参数,因此有些参数在概念上相互链接的可能性很大.而且 other 函数将要使用那些链接的参数的可能性也很大.

That's 4 parameters, not 13. When you're writing a function, and you see that it's taking a large number of parameters, odds are good that some of those parameters are conceptually linked to each other. And odds are also good that other functions will want to use those linked parameters.

例如,您可能要显示角色的状态栏.执行此功能的功能真的需要 character 吗?不;它只需要统计块.因此,应该使用一个stat阻止对象.

For example, you may want to display a character's stat block. Does the function that does this really need the character? No; it just needs the stat block. So it should take a stat block object.

就像角色的构造函数一样.

Just like the character's constructor does.

这篇关于改进13参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆