【问题标题】:How to add method overriding & overloading to Bank Account in Python Tkinter如何在 Python Tkinter 中向银行帐户添加方法覆盖和重载
【发布时间】:2017-07-20 11:20:21
【问题描述】:

我对 python 很陌生,更不用说 Tkinter,并且正在创建一个面向对象的银行帐户,我有一个工作程序,但是在设计应用程序时我计划将 OOP 的所有功能添加到代码中,我将如何添加方法覆盖和重载该程序,同时保持其全部功能?提供您的见解。

银行账户代码

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()

【问题讨论】:

  • 您的问题不清楚,也太宽泛了。为什么只是在子类中创建一个与您要求的不同的方法?您究竟想要覆盖和重载什么?
  • 我的代码是否已经实现了方法重载和覆盖?如果是这样,您能否突出显示请在哪里,就像我说的那样,我是新人。

标签: python oop inheritance tkinter overloading


【解决方案1】:

函数重载(也称为方法重载)是一种编程概念,它允许程序员定义两个或多个具有相同名称和相同作用域的函数。

您的代码中已经有一些“重载”:

class InterestAccount(Account):
    def __init__(self, init_balance=0, rate=0.1):

当创建一个新的InterestAccount 对象时,可以使用 0、1 或 2 个参数调用它,因为指定了这些默认值。正如this SO answer 中提到的,Python 是动态类型的,因此您不需要像在 Java 中那样创建多个具有不同参数的相同方法。

重写是一种面向对象的编程特性,它使子类能够为已经在其父类中定义和/或实现的方法提供不同的实现...

您有很好的机会覆盖InterestAccount 类中的depositwithdraw 方法,因为它继承自Account 并且当前使用其父级对这些方法的实现。只需在 InterestAccount 类中定义 depositwithdraw 方法,但做一些与在父类中所做的不同的事情。

【讨论】:

  • 你能给我看一个编码的例子吗,我更像是一个视觉学习者,我可以如何覆盖,这会很有帮助。
  • @MohamedIsmail 它已经在你的代码中完成了。将depositwithdraw 方法从Account 复制并粘贴到InterestAccount,然后更改这两种方法以适应兴趣。您的Account.get_balance 方法也有错误。应该是def get_balance(self): return self.balance
  • 非常感谢 nelson,您的帮助很大
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多