【发布时间】:2012-05-24 12:41:31
【问题描述】:
我是 python 和一般编程的新手,所以非常感谢任何关于这一点的澄清。
例如在下面的代码中:
#Using a class
class Monster(object):
def __init__(self, level, damage, duration):
print self
self.level = level
self.damage = damage
self.duration = duration
def fight(self):
print self
print "The monster's level is ", self.level
print "The monster's damage is ", self.damage
print "The attack duration is ", self.duration
def sleep(self):
print "The monster is tired and decides to rest."
x = Monster(1, 2, 3)
y = Monster(2, 3, 4)
x.fight()
y.fight()
y.sleep()
#just using a function
def fight_func(level, damage, duration):
print "The monster's level is ", level
print "The monster's damage is ", damage
print "The attack duration is ", duration
fight_func(1, 2, 3)
fight_func(5,3,4)
纯函数版本看起来更干净,结果也一样。
是您可以在对象上创建和调用许多方法的类的主要值,例如打架还是睡觉?
【问题讨论】:
-
这可能会有所帮助。这是wikipedia article on cohesion。