这是我的意思的一个非常简短的例子。
class class1():
def method1(self):
self.variable1 = 5
class class2():
def method2(self):
self.variable2 = var1.variable1 * 2
class class3():
def __init__(self):
self.var1 = class1()
self.var2 = class2()
instance = class3()
instance.var1.variable1 = 10
instance.var2.method2()
print instance.var2.variable2
上面的代码显然会在倒数第二行停止,因为class2不知道var1是什么。
现在我知道这样的东西可以用于子类,或者只是将方法保存在同一个类下。但在我的情况下,我不认为(我不是说我知道,因为我不是很有经验,但我不认为)这是一个可行的解决方案。
这是一个bigger sample code(我实际上正在使用的)并不是全部,还有其他2个(甚至更大的)系统,而不是我需要实现的(技能和津贴)。因此,如果我依赖于子类,我会有一个大型类加载子类而不是后来在代码中组合的许多类。
有什么好方法可以解决这个问题?我只是做错了吗?
编辑/更新:另一个注意事项,这段代码将被多次重复使用,用于许多不同的变量,而不仅仅是一个(当然。)
答案 0 :(得分:1)
也许是这样的:
class NotEnoughMagic(Exception):
pass
class Character(object):
fire = 0
magic = 0
hp = 100
armor = 1
def take_damage(self, amount):
self.hp -= amount
if self.hp <= 0:
print '%s HAS DIED!' % type(self).__name__
def walk(self):
pass
def attack(self, target, attack_points):
target.take_damage(attack_points / target.armor)
class Monster(Character):
fire = 50
hp = 150
armor = 2
def punch(self, target):
self.attack(target, 25)
class Magician(Character):
magic = 10
def magic_armor(self):
self.hp += 20
self.magic -= 10
def sword_attack(self, target):
self.attack(target, 20)
def magic_attack(self, target):
"""Does tons of damage but uses all magic"""
if self.magic > 0:
self.attack(target, 100)
self.magic = 0
else:
raise NotEnoughMagic()
用法:
>>> monster = Monster()
>>> magician = Magician()
>>>
>>> print magician.hp
100
>>> print monster.hp
150
>>>
>>> monster.punch(magician)
>>> magician.sword_attack(monster)
>>>
>>> print magician.hp
75
>>> print monster.hp
140
>>>
>>> monster.punch(magician)
>>> magician.magic_attack(monster)
>>>
>>> print magician.hp
50
>>> print monster.hp
90
>>>
>>> monster.punch(magician)
>>> magician.magic_attack(monster)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 14, in magic_attack
__main__.NotEnoughMagic
>>>
>>> print magician.hp
25
>>> print monster.hp
90
>>>
>>> monster.punch(magician)
Magician HAS DIED!
>>> magician.sword_attack(monster)
>>>
>>> print magician.hp
0
>>> print monster.hp
80
一些潜在的游戏逻辑:
>>> # Gang of monsters
... monsters = [Monster() for x in range(10)]
>>> magician = Magician()
>>>
>>> # Prepare for battle
... while magician.magic > 0:
... magician.magic_armor()
...
... # Put on some chain mail
... magician.armor += 5
File "<stdin>", line 6
magician.armor += 5
^
SyntaxError: invalid syntax
>>>
>>>
>>> while magician.hp > 0:
... for m in monsters:
... m.punch(magician)
...
Magician HAS DIED!
Magician HAS DIED!
Magician HAS DIED!
Magician HAS DIED!
Magician HAS DIED!
Magician HAS DIED!
Magician HAS DIED!
>>>
更新
另外,需要注意的一点是mixins。例如,如果要向一组广泛的类添加其他功能,而不是在整个继承树中添加。
class TeleportAbilityMixin(object):
def teleport(self, destination):
cost = 20
if self.mp >= cost:
# Unpack the x, y coordinates
self.x, self.y = destination
self.mp -= cost
class TeleportingMagician(Magician, TeleportAbilityMixin):
pass
m = TeleportingMagician()
new_location = (10, 5)
m.teleport(new_location)