想要获得类的窍门,任何人都可以告诉我如何让我的方法“攻击”来增加基础伤害?目前它只是打印“伤害”并且不会将baseDamage添加到它。如何将属性传递给Python中的类中的方法?
from random import randint
class Weapon:
def __init__(self, name, baseDamage, price):
self.name = name
self.baseDamage = baseDamage
self.price = price
def attack(self):
damage = randint(1,10)
self.baseDamage + damage
return damage
def main():
steelSword = Weapon("Steel Longsword", 10, 250)
print("Name:", steelSword.name, "\nDamage:", steelSword.baseDamage, "\nPrice:", steelSword.price,"coins")
print(steelSword.attack())
main()
由于您删除了您的其他问题:我强烈建议您撰写涵盖面向对象编程基础知识的教程。 [如何像计算机科学家一样思考 - 用Python学习](http://openbookproject.net/thinkcs/python/english2e/index.html)很不错,请看第13章。类和对象](http://openbookproject.net/thinkcs/python/english2e/ch13.html)和以下两章。 –