gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:30 25 4
gpt4 key购买 nike

我们在计算机科学类(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com