【问题标题】:How to call functions from one subclass to another subclass如何从一个子类调用函数到另一个子类
【发布时间】:2017-05-01 13:47:12
【问题描述】:
class Acct:
    def __init__(self, deposit):
        self.balance = deposit
    def balance(self):
        print("Your balance is $",self.balance)
    def getDeposit(self, deposit):
        self.balance = self.balance + deposit
        print("Your new balance is $",self.balance)
    def getWithdraw(self, withdraw):
        self.balance = self.balance - withdraw
        print("Your new balance is $",self.balance)

class ChkAcct(Acct):
    def __init__(self, deposit):
        super().__init__(deposit)

class SavAcct(Acct):
    def __init__(self, deposit):
        super().__init__(deposit)

savings_account_starting_balance = float(input("Enter a starting balance for your savings account :"))
savings_account = SavAcct(savings_account_starting_balance)
savings_account.balance()       

checking_account_starting_balance = float(input("Enter a starting balance for your checking account :"))
checking_account = ChkAcct(checking_account_starting_balance)
checking_account.balance()

savings_account.getDeposit(float(input("Enter a deposit ammout for savings account :")))

checking_account.getDeposit(float(input("Enter a deposit ammout for checking account:")))

savings_account.getWithdraw(float(input("Enter a withdraw ammout from savings:")))

checking_account.getWithdraw(float(input("Enter a withdraw ammout from checking:")))

我需要创建 2 个类 ChkAcctSavAcct。每个类都应该有一个balance 属性。每个类都应该有一个deposit 方法。每个类都应该有一个withdraw 方法。每个类还应该有一个transfer 方法,该方法调用自己的withdraw 方法并从其他类调用deposit 方法。

我似乎无法弄清楚如何制作传输方法。

【问题讨论】:

  • 如果你想从一个账户取钱,然后放到另一个账户上,你应该使用instances。例如,checking_acount.deposit(savings_account.withdraw(amount))。拥有一个针对特定其他类的方法实际上没有意义。
  • 给该方法一个其他类的实例,并在其上调用withdraw/deposit方法。
  • 正如@Carcigenicate 建议的那样,您将拥有例如checking_account.transfer(amount, savings_account)。这意味着子类不必相互了解太多,只需它们共享Acct 接口即可。

标签: python python-3.x subclass invoke subclassing


【解决方案1】:

有很多方法可以实现这一点。 这是一个:

class Acct:
    def __init__(self, deposit):
        self.balance = deposit

    # Try to avoid same names for methods and properties, unless you have a good reason for it
    # Otherwise you may end up with an error like this: "TypeError: 'float' object is not callable",
    # when you try to call your original balance() method
    # So I renamed it to getBalance()
    def getBalance(self):
        print("Your balance is $",self.balance)

    def getDeposit(self, deposit):
        self.balance = self.balance + deposit
        print("Your new balance is $",self.balance)

    def getWithdraw(self, withdraw):
        self.balance = self.balance - withdraw
        print("Your new balance is $",self.balance)

    # Transfer 'amount' from current instance to 'destination' instance
    def transfer(self, amount, destination):
        self.getWithdraw(amount)
        destination.getDeposit(amount)

class ChkAcct(Acct):
    def __init__(self, deposit):
        super().__init__(deposit)


class SavAcct(Acct):
    def __init__(self, deposit):
        super().__init__(deposit)


# Set up the accounts and fund them
print("1. Setting accounts")
savings_account = SavAcct(100.00)
checking_account = ChkAcct(200.00)

savings_account.getBalance()
checking_account.getBalance()

# Now do the transfer
print("2. Transferring money")
savings_account.transfer(50.00, checking_account)

输出将是这样的:

# 1. Setting accounts
# Your balance is $ 100.0
# Your balance is $ 200.0
# 2. Transferring money
# Your new balance is $ 50.0
# Your new balance is $ 250.0

另一种方法是为此设置一个独立的功能:

def transfer(amount, origin, destination):
    origin.getWithdraw(amount)
    destination.getDeposit(amount)

然后调用它:

transfer(50.00, savings_account, checking_account)

【讨论】:

    【解决方案2】:
    class Acct:
        def __init__(self, deposit):
            self.balance = deposit
        def getBalance(self):
            print("Your new "+str(self.__class__.__name__)+" balance is $",self.balance)
        def getDeposit(self, deposit):
            self.balance = self.balance + deposit
            print("Your new "+str(self.__class__.__name__)+" balance is $",self.balance)
        def getWithdraw(self, withdraw):
            self.balance = self.balance - withdraw
            print("Your new "+str(self.__class__.__name__)+" balance is $",self.balance)
        def getTransfer (self, account, destination):
            self.getWithdraw(account)
            destination.getDeposit(account)
    
    class ChkAcct(Acct):
        def __init__(self, deposit):
            super().__init__(deposit)
    
    class SavAcct(Acct):
        def __init__(self, deposit):
            super().__init__(deposit)
    
    savings_account_starting_balance = float(input("Enter a starting balance for your savings account :"))
    new_savings_account = SavAcct(savings_account_starting_balance)
    new_savings_account. getBalance()
    checking_account_starting_balance = float(input("Enter a starting balance for your checking account :"))
    new_checking_account = ChkAcct(checking_account_starting_balance)
    new_checking_account. getBalance()
    new_savings_account.getDeposit(float(input("Enter a deposit ammout for savings account :")))
    new_checking_account.getDeposit(float(input("Enter a deposit ammout for checking account :")))
    new_savings_account.getWithdraw(float(input("Enter a withdraw ammout from savings :")))
    new_checking_account.getWithdraw(float(input("Enter a withdraw ammout from checking :")))
    new_checking_account.getTransfer(float(input("Enter a transfer ammount from checking to savings :")),new_savings_account)
    new_savings_account.getTransfer(float(input("Enter a transfer ammount from savings to checking :")),new_checking_account)
    

    感谢一位朋友,我最终得到了这个。我喜欢在输出中回调类名以帮助用户区分帐户余额的额外触感

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多