How to add method overriding & overloading to Bank Account in Python Tkinter
我对python非常陌生,更不用说Tkinter了,我正在创建一个面向对象的银行帐户,我有一个工作程序,但是在设计我计划将OOP的所有特性添加到代码中的应用程序时,我如何在保持其全部功能的同时向该程序添加方法重写和重载?给出你的见解。
银行账户代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | from tkinter import * from random import randint import time class Account: def __init__(self, init_balance=0): self.balance = init_balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount def get_balance(self, init_balance, rate): return self.get_balance() * self._rate class InterestAccount(Account): def __init__(self, init_balance=0, rate=0.1): super().__init__(init_balance) self._rate = rate def interest(self): return self.balance * self._rate class GUI(Tk): def __init__(self): Tk.__init__(self) self.title('Bank Account') #Menu# menu = Menu(self) acct_type_menu = Menu(menu) menu.add_cascade(label='Account Type', menu=acct_type_menu) acct_type_menu.add_command(label='Standard', command=self.set_type_standard) acct_type_menu.add_command(label='Interest', command=self.set_type_interest) self.config(menu=menu) #Account# start_balance = randint(100, 500) self.acct = Account(start_balance) self.my_interest = InterestAccount(start_balance) self.interest = self.my_interest.balance + self.my_interest.interest() #Labels# Label(self, text='Current Balance:').pack() self.balance_label = Label(self, text='Error: Select account type') self.balance_label.pack() #Button# btns_frame = Frame(self) btns_frame.pack(side=TOP, fill=X) Button(btns_frame, text='Deposit', width=13, command=self.deposit).pack(side=LEFT) Button(btns_frame, text='Withdraw', width=13, command=self.withdraw).pack(side=RIGHT) #Textbox# vcmd = (self.register(self.onValidate), '%S') self.text = Entry(self, validate='key', vcmd=vcmd) self.text.pack() def onValidate(self, S): if S in '0123456789.': return True return False def set_type_standard(self): self.acct_type = 'standard' self.balance_label.config(text=round(self.acct.balance, 2)) def set_type_interest(self): self.acct_type = 'interest' self.balance_label.config(text=round(self.interest, 2)) def clear_entry(self): self.text.delete(0, END) def deposit(self): if self.acct_type == 'interest': a = int(self.text.get()) self.interest += a self.balance_label.config(text=round(self.interest, 2)) elif self.acct_type == 'standard': a = int(self.text.get()) self.acct.balance += a self.balance_label.config(text=round(self.acct.balance, 2)) else: self.balance_label.config(text='Error: Select account type') self.clear_entry() def withdraw(self): if self.acct_type == 'interest': a = int(self.text.get()) self.interest -= a self.balance_label.config(text=round(self.interest, 2)) elif self.acct_type == 'standard': a = int(self.text.get()) self.acct.balance -= a self.balance_label.config(text=round(self.acct.balance, 2)) else: self.balance_label.config(text='Error: Select account type') self.clear_entry() if __name__ == '__main__': GUI().mainloop() |
Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name and in the same scope.
号
您的代码中已经有了一些"重载":
1 2 | class InterestAccount(Account): def __init__(self, init_balance=0, rate=0.1): |
在创建新的
Overriding is an object-oriented programming feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class...
号
您有很好的机会在您的