我有一个类,Bond
,它有一个我们将投资的期限,一定的投资金额,最低价格,最低期限和年利率。LongTerm
类别的最低期限为2年,最低金额为$250,年息为2.5%。
以下代码可以正常工作:
class Bond(object):
def __init__(self,minTerm, term,amount,minPrice,yrlyRate):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minTerm = minTerm
self.yrlyRate = yrlyRate
class LongTerm(Bond):
def __init__(self, term,amount,minPrice, minAmount = 1000, minTerm = 5, yrlyRate = 0.05):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minAmount = minAmount
self.minTerm = minTerm
self.yrlyRate = yrlyRate
但是当我使用部分继承函数
时super().__init__(term,amount,minPrice)
显示错误:
TypeError: __init__() missing 2 required positional arguments: 'minPrice' and 'yrlyRate'
代码使用super()
:
class Bond(object):
def __init__(self,minTerm, term,amount,minPrice,yrlyRate):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minTerm = minTerm
self.yrlyRate = yrlyRate
class LongTerm(Bond):
def __init__(self, term,amount,minPrice, minAmount = 1000, minTerm = 5, yrlyRate = 0.05):
super().__init__(term,amount,minPrice)
self.term = term
self.amount = amount
self.minPrice = minPrice
为什么我不能部分继承超类的一些实例?
如果子类需要的信息比超类少,那么可能不需要使用继承。在无法修改原始类的情况下,也许组合会更有帮助。
方法1。具有通用类继承
class Bond:
def __init__(self, minTerm, term, amount, minPrice, yrlyRate):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minTerm = minTerm
self.yrlyRate = yrlyRate
class LongTerm(Bond):
def __init__(self, term, amount, minPrice, minAmount = 1000, minTerm = 5, yrlyRate = 0.05):
super().__init__(minTerm, term, amount, minPrice, yrlyRate)
self.minAmount = minAmount
方法2。使用重写示例和默认参数
class Bond:
def __init__(self, minTerm, term, amount, minPrice, yrlyRate):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minTerm = minTerm
self.yrlyRate = yrlyRate
def short_term_only(self):
#do some stuff
class LongTerm(Bond):
def __init__(self, term, amount, minPrice, minAmount = 1000, minTerm = 5, yrlyRate = 0.05):
# In the event that maybe we don't care about some of the information or methods
# in the parent we can set them to none or even over-ride the methods
super().__init__(minTerm, None, None, minPrice, yrlyRate)
self.minAmount = minAmount
def short_term_only(self):
raise NotImplementedError
方法3。class composition
这个方法的问题是,Bond
类可用的方法本质上不会对LongTerm
类可用:
class LongTerm:
def __init__(self, term, amount, minPrice, minAmount = 1000, minTerm = 5, yrlyRate = 0.05):
self.bond = Bond(minTerm, term, amount, minPrice, yrlyRate)
self.minAmount = minAmount
我想你需要的是这样的东西…
class Bond:
def __init__(self, term, amount, minPrice, minTerm, yrlyRate):
self.term = term
self.amount = amount
self.minPrice = minPrice
self.minTerm = minTerm
self.yrlyRate = yrlyRate
def __repr__(self):
return f"Term: {self.term}, Amount: {self.amount}, MinPrice: {self.minPrice}, MinTerm: {self.minTerm}, YearlyRate: {self.yrlyRate}"
class LongTerm(Bond):
def __init__(self, term, amount, minPrice, minTerm=5, yrlyRate=0.05, minAmount=1000):
super().__init__(term=term, amount=amount, minPrice=minPrice, minTerm=minTerm, yrlyRate=yrlyRate)
self.minAmount = minAmount
print(LongTerm(term=1, amount=23, minPrice=45.2))