class tame_dilo:
torpor = 250
def __init__(self, name, effect):
self.name = name
self.effect = effect
def attack(self):
self.torpor = self.torpor - self.effect
dilo = tame_dilo('dilo', 25)
dilo.attack()
print(dilo.torpor)
class tame_sable(tame_dilo):
torpor = 500
sable = tame_sable('sable', 25)
sable.attack()
print(sable.torpor)
我刚开始在python上学习一些oop,我决定做这个小项目来练习一下。
我想知道的是,如果我用正确的方法把这个生物的名字和它的触角联系起来,我会使用继承和一些多态性来定义一个不同的生物,根据造物者类。
另外,我想知道什么是正确的方法,这样用户就可以改变攻击方法的效果,就像你使用更好的平衡来击倒生物一样。
发布于 2017-12-23 18:46:52
迪洛和紫貂是一种驯服。它们是实例,而不是类。
因此,您需要一个能够保存不同属性的类。
另外,假设torpor是健康的,或者是能量,我不知道为什么攻击功能会影响自身。一个例子不应该攻击别的东西吗?
class Tame:
def __init__(self, name, effect, torpor):
self.name = name
self.effect = effect
self.torpor = torpor
def attack(self, other):
other.torpor -= self.effect
现在,创建命名实例。
dilo = Tame('dilo', 25, 250)
sable = Tame('sable', 25, 500)
dilo.attack(sable)
print(sable.torpor)
若要更改驯服的效果,只需更新它。
dilo.effect += 10
https://stackoverflow.com/questions/47954904
复制相似问题