如何在 Python 中初始化继承的 class

How to initialise an inherited class in Python

我很难理解如何在 python OOP 中初始化继承的 class。

我无法弄清楚初始化时需要传递哪些参数。这些是我正在使用的 classes:

class BankAccount:  #parent class

    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def withdrawal(self, withdraw):
        if withdraw > self.balance:
            raise RuntimeError('Sorry, Insufficient Funds!')
        else:
            print('Withdrawal accepted.')
            self.balance -= withdraw
            show_balance = input('See account balance? enter y or n: ')
            if show_balance == 'y':
                print(self.balance)

    def deposit(self, amt):
        self.balance += amt
        print('Deposit Accepted')
        show_balance = input('See account balance? enter y or n: ')
        if show_balance == 'y':
            print(self.balance)


class MinimumBalanceAccount(BankAccount):        #child class

    minimum_balance = 100

    def __init__(self):
        BankAccount.__init__(self)

    def withdrawal(self, withdraw):
        if self.balance - withdraw < self.minimum_balance:
            print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
        else:
            self.balance -= withdraw

但是当我尝试初始化 child class:

acc2 = MinimumBalanceAccount('Milind', 1000)    # I am not sure what to pass as arguments here

Python 给我这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-9-85e55fb15340> in <module>
----> 1 acc2 = MinimumBalanceAccount('milind', 1000)

TypeError: __init__() takes 1 positional argument but 3 were given

我要传递什么作为参数??怎么了?

当您定义 __init__ 函数并将其传递给父级时,您还需要将初始化参数添加到您的子级 Class。

class MinimumBalanceAccount(BankAccount):        #child class

    minimum_balance = 100

    def __init__(self, owner, balance):
        BankAccount.__init__(self, owner, balance)

您需要将所需的参数传递给子类和超类:

class BankAccount:

    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def withdrawal(self, withdraw):
        if withdraw > self.balance:
            raise RuntimeError('Sorry, Insufficient Funds!')
        else:
            print('Withdrawal accepted.')
            self.balance -= withdraw
            show_balance = input('See account balance? enter y or n: ')
            if show_balance == 'y':
                print(self.balance)

    def deposit(self, amt):
        self.balance += amt
        print('Deposit Accepted')
        show_balance = input('See account balance? enter y or n: ')
        if show_balance == 'y':
            print(self.balance)


class MinimumBalanceAccount(BankAccount):

    minimum_balance = 100

    def __init__(self, owner, balance):
        super().__init__(owner, balance)
        self.minimum_balance = MinimumBalanceAccount.minimum_balance

    def withdrawal(self, withdraw):
        if self.balance - withdraw < self.minimum_balance:
            print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
        else:
            self.balance -= withdraw

acc2 = MinimumBalanceAccount('Milind', 1000)

在这种情况下,正如@Deceze 在评论中指出的那样,您可以完全省略 __init__

class MinimumBalanceAccount(BankAccount):        #child class

    minimum_balance = 100

    def withdrawal(self, withdraw):
        if self.balance - withdraw < self.minimum_balance:
            print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
        else:
            self.balance -= withdraw

class 最低余额账户(银行账户):#child class

minimum_balance = 100

def __init__(self,owner, balance):
    BankAccount.__init__(self,owner,balance)

def withdrawal(self, withdraw):
    if self.balance - withdraw < self.minimum_balance:
        print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
    else:
        self.balance -= withdraw