用不同的属性值制作多个 类:有什么方法可以避免制作多个 __init__?
Making several classes with different values for their attributes: any way to avoid having to make several __init__?
我正在 Python 3 中制作基于文本的冒险游戏,我需要为不同的敌人类型制作 类。每种敌人类型都有 5 个属性(当前 hp、最大 hp、攻击、速度和准确度)。我想知道是否有任何方法可以使这些 类 而不必编写包含相同属性声明的几个初始化 ,如果这有意义的话。所以我可以有一个超类或包含这个 __init__
的东西
def __init__(self, hp, attack, speed, accuracy):
#defines stats for the enemy
self.maxhp = hp
self.currenthp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
然后 "copy it" 变成不同的敌人类型 类。有什么办法可以做到这一点,还是我必须为每种敌人类型键入一个包含此初始化的不同超类?
据我了解,您需要一个超类 "Enemy",并且对于 "Enemy" 的每个子类,您需要例如 "Warrior"、"Dragon"、"Knight";每个敌人都有 hp、攻击、准确度统计数据,但 Warrior 可能具有与飞龙不同的属性和方法。在这种情况下,我会查看这篇关于继承的文章。继承允许您只创建一个超类并让每个子类具有这些属性 'inherited':https://www.w3schools.com/python/python_inheritance.asp
例如:
class Subclass(Superclass):
def __init__(self, SubclassAttribute1, SubclassAttribute2):
super().__init__(SuperclassAttribute1, SuperclassAttribute2)
对于您所描述的内容,您实际上 不需要 子 class,您可以拥有 Enemy
的不同实例class。如果您确定有多个 classes,您可以将属性烘焙到 __init__
方法中。这是两个选项...
实例化
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
Warrior = Enemy(100, 5, 7, 6)
Dragon = Enemy(500, 8, 2, 4)
子classes
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
class Warrior(Enemy):
def __init__(self):
super().__init__(100, 5, 7, 6)
class Dragon(Enemy):
def __init__(self):
super().__init__(500, 8, 2, 4)
我希望这能回答您的问题。我会使用第一种方法,直到我真的需要更多特定于每个 class 的函数。从一开始我就不确定 Dragon
比 Enemy
还能做些什么。如果您感到困惑,请在下方评论。
编辑:刚刚想到的第三件事是修改实例化方法。
实例化方法的问题是,如果你想要一种类型的多个,那么你必须不断地用它们的 "Enemy" 属性重新声明它们。但是您可以使用 __call__
方法来避免这种情况。
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
def __call__(self):
return Enemy(self.hp, self.attack, self.speed, self.accuracy)
Warrior = Enemy(100, 5, 7, 6)
Dragon = Enemy(500, 8, 2, 4)
然后您可以在不使用 Enemy(...)
的情况下创建 Warrior
或 Dragon
的新实例:
warrior1 = Warrior()
warrior2 = Warrior()
dragon1 = Dragon()
assert warrior1 is not warrior2
assert dragon1 is not Dragon
assert isinstance(dragon1, Enemy)
我正在 Python 3 中制作基于文本的冒险游戏,我需要为不同的敌人类型制作 类。每种敌人类型都有 5 个属性(当前 hp、最大 hp、攻击、速度和准确度)。我想知道是否有任何方法可以使这些 类 而不必编写包含相同属性声明的几个初始化 ,如果这有意义的话。所以我可以有一个超类或包含这个 __init__
def __init__(self, hp, attack, speed, accuracy):
#defines stats for the enemy
self.maxhp = hp
self.currenthp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
然后 "copy it" 变成不同的敌人类型 类。有什么办法可以做到这一点,还是我必须为每种敌人类型键入一个包含此初始化的不同超类?
据我了解,您需要一个超类 "Enemy",并且对于 "Enemy" 的每个子类,您需要例如 "Warrior"、"Dragon"、"Knight";每个敌人都有 hp、攻击、准确度统计数据,但 Warrior 可能具有与飞龙不同的属性和方法。在这种情况下,我会查看这篇关于继承的文章。继承允许您只创建一个超类并让每个子类具有这些属性 'inherited':https://www.w3schools.com/python/python_inheritance.asp
例如:
class Subclass(Superclass):
def __init__(self, SubclassAttribute1, SubclassAttribute2):
super().__init__(SuperclassAttribute1, SuperclassAttribute2)
对于您所描述的内容,您实际上 不需要 子 class,您可以拥有 Enemy
的不同实例class。如果您确定有多个 classes,您可以将属性烘焙到 __init__
方法中。这是两个选项...
实例化
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
Warrior = Enemy(100, 5, 7, 6)
Dragon = Enemy(500, 8, 2, 4)
子classes
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
class Warrior(Enemy):
def __init__(self):
super().__init__(100, 5, 7, 6)
class Dragon(Enemy):
def __init__(self):
super().__init__(500, 8, 2, 4)
我希望这能回答您的问题。我会使用第一种方法,直到我真的需要更多特定于每个 class 的函数。从一开始我就不确定 Dragon
比 Enemy
还能做些什么。如果您感到困惑,请在下方评论。
编辑:刚刚想到的第三件事是修改实例化方法。
实例化方法的问题是,如果你想要一种类型的多个,那么你必须不断地用它们的 "Enemy" 属性重新声明它们。但是您可以使用 __call__
方法来避免这种情况。
class Enemy:
def __init__(self, hp, attack, speed, accuracy):
self.hp = hp
self.attack = attack
self.speed = speed
self.accuracy = accuracy
def __call__(self):
return Enemy(self.hp, self.attack, self.speed, self.accuracy)
Warrior = Enemy(100, 5, 7, 6)
Dragon = Enemy(500, 8, 2, 4)
然后您可以在不使用 Enemy(...)
的情况下创建 Warrior
或 Dragon
的新实例:
warrior1 = Warrior()
warrior2 = Warrior()
dragon1 = Dragon()
assert warrior1 is not warrior2
assert dragon1 is not Dragon
assert isinstance(dragon1, Enemy)