python - 为什么覆盖我的方法会导致我的属性停止正常工作?

标签 python python-3.x class oop polymorphism

我们在计算机科学类(class)中一直在学习面向对象编程,但作为示例提供的代码似乎无法正常工作。

class bankAccount(): 

    '''This is a bank account class'''

    def __init__(self, account_name = "Bank Account", balance = 1500):
        self.__account_name = account_name 
        self.__balance = balance 

    def deposit(self, value): 
        self.__balance = self.__balance + value 
        print("You now have: ", self.__balance) 

    def withdraw(self, value): 
        self.__balance = self.__balance - value 
        print("You now have: ", self.__balance) 

class currentAccount(bankAccount): 

    '''This is a current account class''' 

    def __init__(self, account_name = "Current Account", balance = 1500): 
        self.__account_name = account_name 
        self.__balance = balance

        super().__init__() 

    def withdraw(self, value): 

        if value > 1000: 
            print("You will have to phone the bank manager") 

        else: 
            self.__balance = self.__balance - value 
            print("You now have: ", self.__balance) 

class savingsAccount(bankAccount): 

    '''This is a current account class''' 

    def __init__(self, account_name = "Savings Account", balance = 1500): 
        self.__account_name = account_name 
        self.__balance = balance 

        super().__init__() 

    def deposit(self, value): 
        self.__balance = self.__balance + (value *1.03) 
        print("You now have: ", self.__balance) 

currentObject = currentAccount() 

savingsObject = savingsAccount() 

while True: 
    print("1. Current Account") 
    print("2. Savings Account")

    menu_option = int(input()) 

    if menu_option == 1: 
        print("1. Deposit funds") 
        print("2. Withdraw funds") 

        submenu_option = int(input()) 

        if submenu_option == 1: 
            value = int(input("How much would you like to deposit? ")) 
            currentObject.deposit(value) 

        elif submenu_option == 2: 
            value = int(input("How much would you like to withdraw? ")) 
            currentObject.withdraw(value) 
        else: 
            print("Wrong menu choice!") 

    elif menu_option == 2: 
        print("1. Deposit funds") 
        print("2. Withdraw funds")

        submenu_option = int(input()) 

        if submenu_option == 1: 
            value = int(input("How much would you like to deposit? ")) 
            savingsObject.deposit(value) 

        elif submenu_option == 2: 
            value = int(input("How much would you like to withdraw? ")) 
            savingsObject.withdraw(value) 

        else: 
            print("Wrong menu choice!") 

    else: 
        print("Wrong menu choice!") 

input() 

似乎每次取款和存款都会存储单独的数据。例如,如果您从储蓄账户存入 100 美元,然后提取 100 美元,则程序似乎会默认使用 1500 作为初始余额,而不是从 (1500 + 100) = 1600 然后执行 (1600 - 100)您存款和取款时的余额。

任何帮助将不胜感激,因为 OOP 对我来说是新的,我不明白为什么会发生这种情况。

最佳答案

造成困惑的原因是您使用了self.__balance。属性名称中的双下划线是激活名称修改的特殊情况: What is the meaning of a single and a double underscore before an object name?

要解决您的问题,只需将所有类属性名称切换为单个下划线前缀:

class bankAccount(): 

    '''This is a bank account class'''

    def __init__(self, account_name = "Bank Account", balance = 1500):
        self._account_name = account_name 
        self._balance = balance 

    def deposit(self, value): 
        self._balance = self._balance + value 
        print("You now have: ", self._balance) 

    def withdraw(self, value): 
        self._balance = self._balance - value 
        print("You now have: ", self._balance) 

class currentAccount(bankAccount): 

    '''This is a current account class''' 

    def __init__(self, account_name = "Current Account", balance = 1500): 
        self._account_name = account_name 
        self._balance = balance

        super().__init__() 

    def withdraw(self, value): 

        if value > 1000: 
            print("You will have to phone the bank manager") 

        else: 
            self._balance = self._balance - value 
            print("You now have: ", self._balance) 

class savingsAccount(bankAccount): 

    '''This is a current account class''' 

    def __init__(self, account_name = "Savings Account", balance = 1500): 
        self._account_name = account_name 
        self._balance = balance 

        super().__init__() 

    def deposit(self, value): 
        self._balance = self._balance + (value *1.03) 
        print("You now have: ", self._balance) 

currentObject = currentAccount() 

savingsObject = savingsAccount() 

while True: 
    print("1. Current Account") 
    print("2. Savings Account")

    menu_option = int(input()) 

    if menu_option == 1: 
        print("1. Deposit funds") 
        print("2. Withdraw funds") 

        submenu_option = int(input()) 

        if submenu_option == 1: 
            value = int(input("How much would you like to deposit? ")) 
            currentObject.deposit(value) 

        elif submenu_option == 2: 
            value = int(input("How much would you like to withdraw? ")) 
            currentObject.withdraw(value) 
        else: 
            print("Wrong menu choice!") 

    elif menu_option == 2: 
        print("1. Deposit funds") 
        print("2. Withdraw funds")

        submenu_option = int(input()) 

        if submenu_option == 1: 
            value = int(input("How much would you like to deposit? ")) 
            savingsObject.deposit(value) 

        elif submenu_option == 2: 
            value = int(input("How much would you like to withdraw? ")) 
            savingsObject.withdraw(value) 

        else: 
            print("Wrong menu choice!") 

    else: 
        print("Wrong menu choice!") 

input()

关于python - 为什么覆盖我的方法会导致我的属性停止正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58301392/

相关文章:

c++ - 如何将一个对象声明为我的类的一部分?

python - 如何在 5 分钟后退出 Python 脚本

python - 尝试使用 Python 中的 REST API 在 Atlassian Bitbucket 中创建拉取请求

python - 在模块上运行 2to3.py 后仍然存在错误

python - 通过连接传递额外的参数

java - 泛型类中 T.class 的等价物是什么?

Python virtualenv 没有名为 django 的模块

python - 安装google api python客户端时出错

python - 我应该如何处理与 getter/setter 相关的 Python 列表属性?

html - WordPress 修改正文类