在我的计算机科学课程中,我们一直在学习有关面向对象编程的知识,但作为示例给出的代码似乎无法正常工作。
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 + 100)= 1600再进行(1600-100),则该程序似乎默认返回到使用1500作为初始余额来创建不同存入和提取时的余额。
OOP对我来说是新手,任何帮助将不胜感激,而且我不知道为什么会这样。
答案 0 :(得分:0)
造成混淆的原因是您使用了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()