繁体   English   中英

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

[英]How to initialise an inherited class in Python

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

我无法弄清楚初始化时需要传递什么 arguments 。 这些是我正在使用的类:

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

但是当我尝试初始化子 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

作为 arguments 我应该传入什么? 怎么了?

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

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

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

class MinimumBalanceAccount(BankAccount):        #child class

    minimum_balance = 100

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM