我对 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)
纯函数版本看起来更干净并且给出相同的结果。
是您可以在对象上创建和调用许多方法的类的主要值,例如战斗还是睡觉?