想要獲得類的竅門,任何人都可以告訴我如何讓我的方法「攻擊」來增加基礎傷害?目前它只是打印「傷害」並且不會將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)和以下兩章。 –