Python 基于UML类图创建代码,给我一个错误
我有一个UML图,并制作了这段代码。。我的解释正确吗?我不确定职业武器部分。我得到的建议是武器不会继承铁匠。。 这是我的密码:Python 基于UML类图创建代码,给我一个错误,python,class,inheritance,Python,Class,Inheritance,我有一个UML图,并制作了这段代码。。我的解释正确吗?我不确定职业武器部分。我得到的建议是武器不会继承铁匠。。 这是我的密码: class Character: def __init__(self, namestring): self.name = namestring self.health = 100 self.type = '' def __str__(self): #string operation retu
class Character:
def __init__(self, namestring):
self.name = namestring
self.health = 100
self.type = ''
def __str__(self): #string operation
return f"Name: {self.name}\nHealth: {self.health}\n Type: {self.type}"
class Blacksmith(Character):
def __init__(self, weapon):
super().__init__(name)
super().__init__(health)
self.weapon = weapon
self.type = 'blacksmith'
class Weapon:
def __init__(self, name, Type, damage):
self.name = name
self.type = Type
self.damage = damage
#testing the code
w = Weapon('stygius', 'stygian blade', 63)
b = Blacksmith("sword")
print(b)
我有这个错误
NameError Traceback (most recent call last)
<ipython-input-14-dc0ac5ca43ff> in <module>()
1 #test class instantiation
2 w = Weapon('stygius', 'stygian blade', 63)
----> 3 b = Blacksmith("sword")
4 print(b)
<ipython-input-13-536a28d9bb79> in __init__(self, weapon)
9 class Blacksmith(Character):
10 def __init__(self, weapon):
---> 11 super().__init__(name)
12 super().__init__(health)
13 self.weapon = weapon
NameError: name 'name' is not defined
namererror回溯(最近一次调用)
在()
1#测试类实例化
2 w=武器(“斯泰吉亚斯”、“斯泰吉亚斯之刃”,63)
---->3 b=铁匠(“剑”)
4印刷品(b)
初始(自我,武器)
9级铁匠(字):
10定义初始(自我,武器):
--->11 super().\uuuu init\uuuuuu(名称)
12超级()。_初始__(健康)
13.自我武器
名称错误:未定义名称“name”
撇开参数名称不谈,我想你已经知道了。Blacksmith.\uuu str\uuuu
方法可以从删除多余的空格中获益,例如“Name:{}\nHealth:{}\nType:{}”
@MichaelRuth-hi!我更新了从铁匠继承中移除武器的代码?并且添加了super()。\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。此外,关于铁匠
和武器
也没有继承权,因为不应该有继承权,即铁匠
不是武器
。相反,这是一种组合关系,即铁匠
有一个武器
。始终将问题中的完整错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,也不是指向外部门户的链接)放置(不是注释)。还有其他有用的信息。@furas我现在添加了完整的错误