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