【发布时间】:2020-03-29 19:10:31
【问题描述】:
在不放弃太多的情况下,我正在制作一个具有 9 个属性的对象。让我们以 RPG 为例。
float strength, constitution, fortitude, dexterity, intelligence, charisma, wisdom, willpower, perception;
鉴于该对象被称为播放器,构造函数
public Player(float strength, float constitution, float fortitude, float dexterity,
float intelligence, float charisma, float wisdom, float willpower, float perception){}
根据 SonarLint: "构造函数有9个参数,大于7个授权。"
虽然我知道 SonarLint 说这不会阻止代码实际运行,但如果有处理这些情况的“正确”方法,我很想知道!
谢谢
P.s,如果这个问题的论坛发错了,请指点我正确的!
【问题讨论】:
-
感谢您迄今为止的回复。我将按照建议查找 Builder Pattern 和“EnumMaps”。在确定最适合我的情况后,我会接受最佳答案!
-
如果你不使用
EnumMap,当你开始为这个类添加更多功能时你会后悔的。
标签: java constructor sonarlint