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

如何在Python中初始化继承的类,python,class,oop,inheritance,super,Python,Class,Oop,Inheritance,Super,我很难理解如何在python OOP中初始化继承的类 我无法确定初始化时需要传递哪些参数。以下是我正在使用的类: class BankAccount: #parent class def __init__(self, owner, balance): self.owner = owner self.balance = balance def withdrawal(self, withdraw): if withdraw >

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

我无法确定初始化时需要传递哪些参数。以下是我正在使用的类:

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
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

我应该传递什么作为参数??出了什么问题?

定义uu init uu函数并将其传递给父类时,还需要将初始化参数添加到子类中

class MinimumBalanceAccount(BankAccount):        #child class

    minimum_balance = 100

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

定义_uinit_u函数并将其传递给父类时,还需要将初始化参数添加到子类中

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)

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

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)

类别MinimumBalanceAccountBankAccount:子类别

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

类别MinimumBalanceAccountBankAccount:子类别

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

您还需要接收子类构造函数中的参数,并将其传递给超类。如果init中没有MinimumBalanceAccount的参数,则无法将其传递给BankAccount的init MinimumBalanceAccount。uu init_uuu接受零参数,这就是您看到的。您有三种选择:让MinimumBalanceAccount接受与BankAccount相同的参数,并将它们传递给它;调用BankAccount时,让MinimumBalanceAccount不接受任何参数,但提供自己的默认参数。\uu init\uuuuuuu,让MinimumBalanceAccount不调用BankAccount。\uu init\uuuuu。你可能想要第一个选择。由于MinimumBalanceAccount.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果init中没有MinimumBalanceAccount的参数,则无法将其传递给BankAccount的init MinimumBalanceAccount。uu init_uuu接受零参数,这就是您看到的。您有三种选择:让MinimumBalanceAccount接受与BankAccount相同的参数,并将它们传递给它;调用BankAccount时,让MinimumBalanceAccount不接受任何参数,但提供自己的默认参数。\uu init\uuuuuuu,让MinimumBalanceAccount不调用BankAccount。\uu init\uuuuu。你可能想要第一个选择。因为MinimumBalanceAccount.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果您保留它,您应该使用super。应该注意,这里的uu init uu可以完全省略,因为它不做任何事情。如果你保留它,你应该使用super。或者你应该完全省略MinimumBalanceAccount。是的,我注意到了,谢谢;我认为这将是一个好主意,保持它作为一个超级。。。我添加了最小余额的赋值作为一个实例变量,因此需要使用uu init uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;我认为这将是一个好主意,保持它作为一个超级。。。我添加了最小余额赋值作为实例变量,因此需要uuu init