我是否应该为任何与基本类相同的唯一对象创建一个新的子类'对象除默认值外吗?
我有一个类:
Class Monster
{
private:
const int force;
const int prize;
const int damage;
}
在我的代码中,我希望能够轻松地创建一个我称之为龙的唯一怪物对象,该对象具有所有怪物属性的默认值,而这些怪物的属性无法初始化为不同的值(例如:::所有龙的力= 5,奖品= 10,伤害= 10)
我的问题是 - 龙应该是怪物的子类,即使两者之间没有差异,除了默认值和对象的名称?
(实际上我有很多类型,例如龙 - 每个类型都有其自己的默认值)。
看来这将是我需求的一个很好的解决方案,但是我是新手的OOP,并且不确定它是可以接受的,或者如何以不同的方式却优雅地解决它。
I have a class:
Class Monster
{
private:
const int force;
const int prize;
const int damage;
}
In my code, I want to be able to easily create a unique Monster Object that I call Dragon which has default values for all the Monster's attributes that can't be initialized to different values (for example: all Dragons has force=5, prize=10, damage=10)
My question is- should Dragon be a subclass of Monster even though there is not realy a difference between the two except for the default values and the name of the object?
(In fact I have many types such as Dragon- each has it's own default values).
It seems that this is going to be a fine solution for my needs but I'm new to OOP and not sure it's acceptable or how to solve it differently, yet elegantly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管这可能不是最干净的解决方案,但您可以为不同的怪物类型创建
enum class
,并使用自定义方法调用构造函数。Although this might not be the cleanest solution, you could create an
enum class
for your different monster types and use a custom method to call the constructor.