【发布时间】:2020-07-18 17:14:37
【问题描述】:
试图从超类 BasicToken 继承到子类 StableCoin
supply 的值没有被 子类继承:StableCoin
谁能解释一下原因?
class BasicToken:
asset_class = "Crypto-Currency"
def __init__(self, symbol):
self.symbol = symbol
class StableCoin(BasicToken):
def __init__(self, color, supply):
self.color = color
self.supply = supply
icon = BasicToken('icx')
icxStable = StableCoin('DMM', ['Blue', 'White'])
print('Icon Symbol: '+ icon.symbol)
print('IcxStable Symbol: '+ icxStable.symbol)
Error:
Traceback (most recent call last):
File "C:/Users/Samson/Documents/PythonCourse-master/Section_04/assignment_04.py", line 30, in <module>
print('IcxStable Symbol: '+ icxStable.symbol)
AttributeError: 'StableCoin' object has no attribute 'symbol'
【问题讨论】:
-
你需要在子类的构造函数中调用超类的构造函数。
super()
标签: python python-3.x inheritance