It's kind of a shame to ask this question and probably will fit better in the Code Review site, so sorry in advance.
提出这个问题并且可能更适合代码审查网站是一种耻辱,所以提前抱歉。
My question is the following (can be extensible for other languages since is more OOP):
我的问题是以下(因为更多的OOP可以扩展为其他语言):
I have a class:
我上课了:
class Unit
{
public:
Unit(Type);
Type type;
private:
int weaponry;
int shielding;
int hull;
int rapid_fire;
}
with an enum to differenciate between different types of units.
用枚举来区分不同类型的单位。
enum Type{
Cruiser,
Missile
};
All the units will be initialize with a default value (plus a factor, depending in external variable).
所有单位都将使用默认值进行初始化(加上一个因子,具体取决于外部变量)。
Unit::Unit(Type type)
{
this->type = type;
int weaponry, shielding, hull,rapid_fire;
switch(type){
case Cruiser:
weaponry = 2700;
shielding = 50;
hull = 400;
rapid_fire = 5;
break;
case Missile:
weaponry = 200;
shielding = 20;
hull = 80;
rapid_fire = 0;
break;
}
this->weaponry = weaponry ; //+ whatever
this->shielding = shielding; //+ whatever
this->hull = hull; //+ whatever
this->rapid_fire = rapid_fire;
}
I will also have a method that will change the values of the object, such as the typical
我还将有一个方法可以更改对象的值,例如典型值
setHull(int newHull){this->hull = newHull}
In one of these methods, i want to revert one of the private variables to its default value, in the example case, if is Cruiser this->shielding = 50, if its a missile = 20.
在其中一种方法中,我想将其中一个私有变量恢复为其默认值,在示例中,如果是Cruiser this-> shielding = 50,如果它的导弹= 20。
My questions are the following. Am i doing something wrong?
我的问题如下。难道我做错了什么?
I have several options to keep the defaults values, either with (the one I would "noobly" will choose)
我有几个选项来保持默认值,或者是(我将“noobly”选择的那个)
#define initial_cruiser_shielding 50
either with enum:
要么枚举:
enum shielding_init{
cruiser_i = 50,
missile_i = 20
};
to have default instances of the basic objects, and then just copy them and create as many new objects I need.
拥有基本对象的默认实例,然后只需复制它们并创建我需要的新对象。
Thanks in advance!
提前致谢!
1 个解决方案
#1
0
My recommendation will be to create private static member functions that can return default values.
我的建议是创建可以返回默认值的私有静态成员函数。
class Unit
{
public:
Unit(Type);
Type type;
int set_default_weaponry()
{
weaponry = get_default_weaponry();
}
int set_default_shielding()
{
shielding = get_default_shielding();
}
int set_default_hull()
{
hull = get_default_hull();
}
int set_default_rapid_fire()
{
rapid_fire = get_default_rapid_fire();
}
private:
int weaponry;
int shielding;
int hull;
int rapid_fire;
static int get_default_weaponry();
static int get_default_shielding();
static int get_default_hull();
static int get_default_rapid_fire();
}
#1
0
My recommendation will be to create private static member functions that can return default values.
我的建议是创建可以返回默认值的私有静态成员函数。
class Unit
{
public:
Unit(Type);
Type type;
int set_default_weaponry()
{
weaponry = get_default_weaponry();
}
int set_default_shielding()
{
shielding = get_default_shielding();
}
int set_default_hull()
{
hull = get_default_hull();
}
int set_default_rapid_fire()
{
rapid_fire = get_default_rapid_fire();
}
private:
int weaponry;
int shielding;
int hull;
int rapid_fire;
static int get_default_weaponry();
static int get_default_shielding();
static int get_default_hull();
static int get_default_rapid_fire();
}