我想从子类访问父类变量'bal',并对其进行更改。你知道吗
我尝试在没有init()函数的情况下使用子类,但它表明deposit不是AccountHolder的属性,AccountHolder是父类。你知道吗
class AccountHolder(object):
total = 0
def __init__(self, acc_no, name, age, bal):
self.acc_no = acc_no
self.name = name
self.age = age
self.bal = bal
AccountHolder.total = AccountHolder.total + 1
def __repr__(self):
return "\nName: {}\nBalance: {:10.2f}\n".format(self.name, self.bal)
def update(self, new_bal):
print"Old balance:", self.bal
self.bal = new_bal
print"New balance:", self.bal
def totalAccHolders():
print"Total no of account holders=", AccountHolder.total
totalAccHolders = staticmethod(totalAccHolders)
class savingah(AccountHolder):
def __init__(self, acc_no, name, age, bal, dep):
super().__init__(acc_no, name, age, bal)
self.dep=dep
def deposit(AccountHolder, new_bal):
print"Old balance:", AccountHolder.bal
self.bal += dep
print"New balance:", AccountHolder.bal
class loanah(AccountHolder):
def __init__(self, acc_no, name, age, bal, wdraw):
super().__init__(acc_no, name, age, bal)
self.wdraw=wdraw
def withdraw(self, new_bal):
print"Old balance:", self.bal
self.bal -= wdraw
print"New balance:", self.bal
listAcc = []
b = savingah()
while(True):
print("\nMENU")
print"1. Create a new AccountHolder"
print"2. Update AccountHolder balance"
print"3. Saving update"
print"4. Loan update"
op = int(input("Enter option - "))
if op==1:
listAcc.append(AccountHolder(int(input("Enter AccNo: - ")),
input("Enter Name: "),
int(input("Enter age: ")),
float(input("Enter balance: "))))
print(listAcc[-1])
AccountHolder.totalAccHolders()
elif op==2:
s = input("Enter Account number - ")
for a in listAcc:
if s == a.acc_no:
bal = float(input("Enter new balance - "))
a.update(bal)
elif op==3:
s = input("Enter Account number - ")
for a in listAcc:
if s == a.acc_no:
bal = float(input("Enter amount to be deposited - "))
b.deposit(bal)
elif op==4:
s = input("Enter Account number - ")
for a in listAcc:
if s == a.acc_no:
bal = float(input("Enter amount to be withdrawn - "))
a.withdraw(bal)
else:
print "Invalid input\n"
break
我用这个代码得到的错误,如果我从savingah中删除init函数,我会得到一个错误,说类AccountHolder init属性不存在-
[student@PC-16 ~]$ python vexp6.py
Traceback (most recent call last):
File "vexp6.py", line 33, in <module>
b = savingah()
TypeError: __init__() takes exactly 6 arguments (1 given)
任何帮助都将不胜感激,谢谢。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐