部分继承

Partial inheritance

我是 Python 的新手,但有些东西让我对 class 继承感到疯狂,我在网上找到的所有解决方案都不适合我,而且我很难理解为什么。

我有一个 class,Bond,它有一个例子,我们将投资于他们的期限,将投资的一定金额,最低价格,最低期限和年利率。 subclass 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

为什么我不能仅部分继承 superclass 的某些实例?

如果你的 subclass 需要的信息比你的 superclass 少,那么你可能不需要使用继承。如果无法修改原始文件,那么 composition 可能会更有帮助。

方法一.一般class继承

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

方法二)。使用覆盖示例和默认参数

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组成。对这种方法的关注是 methods 可用于 class Bond 不会固有地可用于 class 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))