简体   繁体   English

Python,如何在大类下创建特定的类

[英]Python, how to create specific classes under a big class

New python learner here. 这里是新的python学习器。 I'm trying to design a text rpg in python and i want the stats of all the enemy mobs to increase at a specific part of the game. 我正在尝试在python中设计一个文本rpg,我希望在游戏的特定部分增加所有敌方生物的统计数据。 Here is my code for some those enemies: 这是我为那些敌人准备的代码:

class Executioner:
    def __init__(self,name):
    self.name = name

    self.potions = 0
    self.maxhp = 800 
    self.hp = self.maxhp 
    self.attack = 25 
    self.ability = 0 
    self.defense = 0 
    self.goldgain = 333 
    self.expgain = 250 
ExecutionerIG = Executioner("Executioner the slayer")

class Goblin:
    def __init__(self,name):
    self.name = name

    self.potions = 0
    self.maxhp = 80
    self.hp = self.maxhp
    self.attack = 25
    self.ability = 0
    self.defense = 0
    self.goldgain = 5.5
    self.expgain = 22
GoblinIG = Goblin("Goblin")

class Zombie:
    def __init__(self,name):
    self.name = name

    self.potions = 0
    self.maxhp = 180
    self.hp = self.maxhp
    self.attack = 19    
    self.ability = 0
    self.defense = 0
    self.goldgain = 7
    self.expgain = 28
ZombieIG = Zombie("Zombie")

I thought a way to do this quickly can be defining those classes into a big class called enemies and then call enemies.hp, enemies.attack etc to increase. 我认为一种快速实现此目的的方法可以是将这些类定义为一个称为敌人的大类,然后再调用敌人.hp,敌人。攻击等来增加。

But i don't know how to do that either as i am quite weeb for now. 但是我也不知道该怎么做,因为我现在很疲倦。

Suggestions on how to increase class stats by specifying small classes under a big class or ways to increase class stats quickly(just typing them up and increasing them won't work as i will be having lots of enemy mobs) will be greatly thanked. 我们将非常感谢您提出有关如何通过指定大型班级下的小班级来增加班级统计信息或快速增加班级统计信息的建议(仅输入并增加班级统计信息是行不通的,因为我会遇到很多敌人)。

No, that wouldn't be a good way to do it. 不,那不是一个好方法。 Nested classes are rarely useful in Python, and certainly wouldn't be helpful here. 嵌套类在Python中很少有用,并且在这里肯定不会有用。

Much better to simply put them into a list you can loop through: 最好将它们放入可以循环浏览的列表中:

enemies = []
...
ExecutionerIG = Executioner("Executioner the slayer")
enemies.append(ExecutionerIG)

and so on. 等等。 Then, when necessary, just iterate: 然后,在必要时进行迭代:

for enemy in enemies:
    enemy.hp += 1

As an aside, I would question whether you really need separate classes for Executioner, Zombie and Goblin; 顺便说一句,我会质疑您是否真的需要Executioner,Zombie和Goblin的单独类? they only differ in the numbers for each stat, so perhaps you should just accept those values in the __init__ method for a generic Enemy class. 它们的每个统计信息的编号都不同,因此对于一般的敌人类,也许您应该只在__init__方法中接受这些值。

What Daniel Roseman said is true, you should try to not repeat yourself when writing code. 丹尼尔·罗斯曼(Daniel Roseman)所说的是真的,在编写代码时,您应该尽量不要重复自己。 If two objects are very similar and only differ in some certain values, then really they are the same object. 如果两个对象非常相似并且仅在某些特定值上有所不同,那么实际上它们是同一对象。 All bikes are bikes, but some have different wheel sizes for example. 所有自行车都是自行车,但是有些自行车具有不同的车轮尺寸。 Here, your objects are all one Enemy object, and can be implemented as such. 在这里,您的对象都是一个敌人对象,可以照此实现。

I will however answer your question, because subclassing is very important to know. 但是,我将回答您的问题,因为了解子类非常重要。 To subclass a class, you pass in the parent class in a definition, like so: 要对一个类进行子类化,可以在定义中传入父类,如下所示:

class Zombie(Enemy):

Then if the Enemy class is defined like so for example: 然后,如果像这样定义了Enemy类,例如:

class Enemy: def attack(): <your code>

You could call attack on a Zombie object. 您可以对僵尸对象发起攻击。 It will look for attack() in the Zombie class, and if it doesn't find it, it will look for it in the parent class. 它将在Zombie类中寻找Attack(),如果找不到它,它将在父类中寻找它。 If it doesn't find it in the parent class it will look for it in that parent's class and so on, moving all the way up to the top level object. 如果在父类中找不到它,它将在该父类中寻找它,依此类推,一直向上移动到顶层对象。 If it doesn't find it at all an Exception will be raised. 如果根本找不到,则会引发异常。 If you defined attack() in the Zombie class for example, then it would overwrite attack() in the Enemy class. 例如,如果您在Zombie类中定义了Attack(),那么它将覆盖Enemy类中的Attack()。

Given all this though, Daniel Roseman is right. 鉴于所有这些,丹尼尔·罗斯曼是对的。 You should put all this in one class and pass in hp etc upon construction. 您应该将所有这些归为一类,并在构建时传递hp等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM