Python多重继承钻石问题

问题描述 投票:0回答:0

我有一个钻石继承场景。两个中间类继承没问题,但是基类,我不太明白。我希望 Combo 类使用来自 Loan 类的重写 withdraw 方法继承所有属性。

账户是贷款和交易账户。组合账户是指贷款账户附有卡,也是交易账户。

class Account:
    def __init__(self, account_id, customer_id, balance, interest_rate):
        self.account_id = account_id
        self.customer_id = customer_id
        self.balance = float(balance)
        self.interest_rate = float(interest_rate)

    def deposit(self):
        try:
            amount = float(input('Enter deposit amount: '))
            self.balance = self.balance + amount
            print('Account Number:', self.account_id)
            print('Deposit Amount:', amount)          
            print('Closing Balance:', self.balance)
            print('Deposit successful.\n')
            return True
        except ValueError as e:
            print('Error: please enter a valid amount.\n')
            return False

    def withdraw(self):
        try:
            amount = float(input('Enter withdrawal amount: '))
            if amount > self.balance:
                print('Account Number: ', self.account_id)
                print('Withdrawal Amount: ', amount)
                print('Account Balance: ', self.balance)
                print('Error: withdrawal amount greater than balance.\n')
                return False
            else:
                self.bal = self.balance - amount
                print('Account Number: ', self.account_id)
                print('Withdraw Amount: ', amount)
                print('Closing Balance: ', self.balance)
                print('Withdrawal Successful.\n')
                return True
        except ValueError as e:
            print('Error: please enter a valid amount.\n')
            return False

    def __str__(self):
        return (f'\nAccount Number: {self.account_id}\n'
    f'Balance: {self.balance:.2f}\n'
    f'Interest Rate: {self.interest_rate:.2f}\n')


class Transaction(Account):
    def __init__(self, account_id, customer_id, balance, interest_rate, card_no, cvn, pin):
        super().__init__(account_id, customer_id, balance, interest_rate)
        self.card_no = card_no
        self.cvn = cvn
        self.pin = pin
        
    def __str__(self):
        return super().__str__()

    
class Loan(Account):
    def __init__(self, account_id, customer_id, balance, interest_rate, duration, frequency, payment, limit):
        super().__init__(account_id, customer_id, balance, interest_rate)
        self.duration = int(duration)
        self.frequency = frequency
        self.payment = float(payment)
        self.limit = float(limit)

    # override inherited method
    def withdraw(self):
        try:
            amount = float(input('Enter withdrawal amount: '))
            if self.balance - amount < self.limit:
                print('Account Number: ', self.account_id)
                print('Withdrawal Amount: ', amount)
                print('Account Balance: ', self.balance)
                print('Error: withdrawal amount greater than limit.\n')
                return False
            else:
                self.balance = self.balance - amount
                print('Account Number: ', self.account_id)
                print('Withdraw Amount: ', amount)
                print('Closing Balance: ', self.balance)
                print('Withdrawal Successful.\n')
                return True
        except ValueError as e:
            print('Error: please enter a valid amount.\n')
            return False

    def __str__(self):
        return super().__str__() + (f'Duration: {self.account_id} years\n'
    f'Payment Frequency: {self.frequency}\n'
    f'Limit: {self.limit:.2f}\n')


class Combo(Loan, Transaction):
    def __init__(self, account_id, customer_id, balance, interest_rate, duration, frequency, payment, limit, card_no, cvn, pin):
        # what goes here?

    def __str__(self):
        return super().__str__()

测试数据...

from account import Account, Transaction, Loan, Combo

account_id = '1'
customer_id = '1'
balance = 0
interest_rate = 0.06
duration = 20
frequency = 'week'
payment = 500
limit = 500000
card_no = '5274728372688376'
cvn = '234'
pin = '9876'

loan = Loan(account_id, customer_id, balance, interest_rate, duration, frequency, payment, limit)
print(loan)

transaction = Transaction(account_id, customer_id, balance, interest_rate, card_no, cvn, pin)
print(transaction)

# whatever I do, it fails here
combo = Combo(account_id, customer_id, balance, interest_rate, duration, frequency, payment, limit, card_no, cvn, pin)
print(combo)
python multiple-inheritance diamond-problem
© www.soinside.com 2019 - 2024. All rights reserved.