如何将函数从一个子类调用到另一个子类

时间:2017-05-01 13:47:12

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

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方法。

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

2 个答案:

答案 0 :(得分:0)

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

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)

答案 1 :(得分:0)

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)

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