Player 应该继承还是拥有一个关卡? [英] Should Player inherit or own a Level?

查看:49
本文介绍了Player 应该继承还是拥有一个关卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在尽可能多地学习 OOP,并且我学到了很多东西,但我不确定这一点,我的类层次结构应该是什么样的?

I've been trying to learn OOP for the last few weeks as much as I can, and I've learned alot, but I'm not certain of this one, what should my class hierarchy look like?

想象两个类,这是一个Level-class:

Imagine two classes, here's a Level-class:

class Level(object):
    def __init__(self, level, exp):
        self.level = level
        self.exp = exp

    @property
    def required_exp(self, level=None):
        return 80 + 40 * (level or self.level)

    def add_exp(self, exp):
        self.exp += exp
        while self.exp >= self.required_exp:
            self.exp -= self.required_exp
            self.level += 1

显然它还有其他一些方法,但这是基本思想.

Obviously it has some other methods too, but that's the basic idea.

现在第二个类叫做Player.它有一些属性,例如:namehealthlevelexperience pointsdamage

Now the second class is called Player. It has some attributes, such as: name, health, level, experience points, damage, etc.

到目前为止我所学到的(SO 上有人说过这个)是当我不确定是使用继承 (is-a-relationship) 还是属性 (有-a-关系).问题是:玩家是一个关卡,还是玩家有一个关卡?

What I've learned so far (someone on SO said this) is to ask myself a question when I'm not sure whether to use inheritance (is-a-relationship) or attribute (has-a-relationship). The question would be: Is Player a Level, or does Player have a Level?

嗯……两者都不是.播放器肯定不是一个级别,我敢肯定.Player 有它的等级,但等级不等于 Level 类.取而代之的是 Player 有等级和经验值,它们都是 Level 类的属性.您还可以为 Player 添加经验值,因此从 Level 继承它是有意义的.

Well... It's neither. Player certainly is not a level, that I'm sure of. And Player has it's level, but the level is not equal to Level class. Instead Player has level and experience points, which are both attributes of Level class. You can also add experience points to Player, so it would make sense to inherit it from Level.

顺便说一句,这些命名对我来说也没有任何意义.使用类似:Level.increase(self) 将级别增加 1 是有意义的.然而,对于玩家来说,调用 Player.level_up(self) 会更有意义,就像大多数(所有?)RPG 游戏中使用的那样.调用 Player.increase(self) 没有任何意义,你不能将 Player 加一,你是把它的等级加一.再说一次,调用 Level.level_up(self) 也没有多大意义......

As a sidenote, the namings don't really make any sense to me either. It would make sense to have something like: Level.increase(self) to increase a Level by one. However, for player it would make more sense to call Player.level_up(self), as used in most (all?) rpg games. Calling Player.increase(self) doesn't really make any sense, you can't increase Player by one, you're increasing it's level by one. Then again, calling Level.level_up(self) doesn't make that much sense either...

所以,我认为我应该像使用接口一样使用 Level 并从中继承 Player(和其他子类),但我不确定是否如此我决定问你,反正我是来这里学习的.我应该怎么办?将 LevelPlayer 一起使用的最正确方法是什么?

So, I think that I should use Level as if it was an interface and inherit Player (and other subclasses) from it, but I'm not certain so I decided to ask you, since I'm here to learn anyways. What should I do? What would be the most proper way to use Level with Player?

推荐答案

我相信是我让你问自己这个问题的人:A 类是 B 类,还是 A 类有B班?",所以我觉得我必须回答这个问题.

I believe I'm the one who told you to ask yourself the question: "Is class A a class B, or does class A have a class B?", so I feel like I have to answer this question.

我不能 100% 确定您的级别实际上是做什么的,但我假设它与《口袋妖怪》、《魔兽世界》、《英雄联盟》和数千种其他游戏中使用的级别相似.如果是这样,你的 Level-class 已经出错了,它不应该是那样的.

I'm not 100% sure of what your level actually does, but I'm assuming it's one similar to the one used in Pokemon, World of Warcraft, League of Legends, and thousands of other games. If so, you've already gone wrong with your Level-class, it shouldn't be like that.

你肯定在一件事上是对的;玩家肯定不是一个级别"正如其他人已经提到的,您应该使用 has-a 关系.玩家确实有水平,我们都知道.但是,您告诉我玩家没有 Level 类型的属性,而是一个整数 level-属性来保存 Level 的数值-class.

You're right on one thing for sure; "Player certainly is not a level" As others have already mentioned, you should just go with has-a relationship. Player does have level, we both know that. However, you're telling me player doesn't have an attribute of type Level, but rather an integer level-attribute to hold the numeric value of your Level-class.

这应该已经敲响了警钟,但这里有一个提示:它与具有名为 name 的 string-type 属性的 Name-class 相同 在里面.

This should already ring some bells, but here's a tip: It's the same as having a Name-class that has a string-type attribute called name inside it.

所以答案是:Name 类本身应该已经是一个字符串,它应该从 string 继承,而不是在其中包含一个 string 属性来保存它的值.这同样适用于您的 Level,它本身已经是一个整数,因此从 int 继承它,然后添加 exp 及其方法.

So the answer is: Name class should already be a string itself, it should inherit from string, instead of having a string attribute inside it to hold its value. The same applies for your Level, it's already an integer itself so inherit it from int, then add exp and its methods.

现在您可以使用 player.level 返回玩家等级的整数值.为了获得exp,你必须"使用player.level.exp.我引用了必须",因为尽管听起来很奇怪,exp 是关卡的一个属性.基本上和数字的小数点一样,有意义吗?

Now you can use player.level to return the integer value of player's level. And to get the exp, you "have to" use player.level.exp. I quoted the "have to", since even though it might sound weird, exp IS an attribute of level's. It's basically the same as a decimal point for a number, makes sense?

此外,现在调用诸如 player.level.increase() 之类的东西更有意义.虽然,它已经是一个整数值,为什么不直接做 player.level += 1 呢?如果您在添加中需要一些高级内容,您可以覆盖 Level.__add__(self, value) 方法.我想不出一个原因为什么你不需要这个?(不是现在您已经将 required_exp 设为属性)

Also, calling something like player.level.increase() makes much more sense now. Although, it's already an integer value, why not just do player.level += 1? And if you need some advanced stuff inside the addition, you could just override the Level.__add__(self, value) method. I can't think of a single reason why wouldn't you need this though? (not now that you've already made required_exp a property)

这篇关于Player 应该继承还是拥有一个关卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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