我是 Python 新手,但有一些東西讓我對類繼承發瘋,而且我在網上找到的所有解決方案都對我不起作用,而且我無法理解為什么。
我有一個類,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
為什么我不能只部分繼承超類的某些實體?
uj5u.com熱心網友回復:
如果您的子類需要的資訊少于您的超類,那么您可能不需要使用繼承。如果無法修改原件,那么可能composition會更有幫助。
方法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) 用類組合。對這種方法的關注是,methods對類可用的方法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
uj5u.com熱心網友回復:
我想你需要的是這樣的......
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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/334308.html
上一篇:使用DACPAC,當表上有一個觸發器時,我怎樣才能重命名一個列?
下一篇:如何將函式傳遞給dart建構式?
