【发布时间】:2020-05-06 21:50:11
【问题描述】:
我正在学习 C# 并制作了一个简单的“播放器”类。但我很难有多重超载。 这是我最好的解决方案,但我觉得它可以做得更简单/更好。
class Player : Entity
{
public Player() {
Name = "Player";
XP = 0;
LVL = 1;
XPToLvlUp = 10;
XpRank = 10;
}
public Player(string name) : this() {
Name = name;
}
public Player(string name, int _Hp, int _Mp) : this(name) {
HP = _Hp;
MP = _Mp;
}
public Player(string name, int _Hp, int _Mp, int _Xp, int _Lvl) : this(name, _Hp, _Mp) {
XP = _Xp;
LVL = _Lvl;
}
public Player(string name, int _Hp, int _Mp, int _Xp, int _Lvl, int XpByRank) : this(name, _Hp, _Mp, _Xp, _Lvl) {
XpRank = XpByRank;
}
//deleted code for better reading
private int XPToLvlUp;
private int XpRank;
public int XP;
public int LVL;
public string Name;
}
好不好,如果不好请告诉我为什么。 感谢您的回复!
【问题讨论】:
-
我觉得不错。如果你感觉不好,你应该说明为什么这看起来不好,也许最好理解你的疑虑。
-
添加@user8276908 评论:您是否有理由要使用特定的构造函数而不是只使用一个?
标签: c# class constructor-overloading