python中类继承的问题,很简单的代码,求大神帮小弟答疑一下,真心感谢,在线等。
classBankAccount:def__init__(self,name,account):self.name=nameself.account=accountsel...
class BankAccount:
def __init__(self,name,account):
self.name=name
self.account=account
self.balance=0.0
print 'Welcome!',name,account
def displaybalance(self):
print 'Your balance is',self.balance
def save(self,money):
self.balance=self.balance+money
print 'You have save',money,'to your account.'
self.displaybalance()
def take(self,money):
if self.balance>=money:
self.balance=self.balance-money
print 'You have take',money,'from your account.'
self.displaybalance()
else:
print 'You will take '+str(money)+',but your account has not enough money to take.'
print 'Your account only has',self.balance
class InterestAccount(BankAccount):
def add_interest(self,interest_rate,time):
for i in range(time):
interest=self.balance*interest_rate
self.balance=self.balance+interest
print 'You have saved for',time,'years.'
print 'Add interest to your account:'+'('+str(self.account)+')',interest_rate*100,'percent'
print 'Now your balance is:',self.balance
YQaccount=InterestAccount('YQ',18695997416)
YQaccount.save(450000)
YQaccount.add_interest(0.03,5)
YQaccount.take(200000)
YQaccount.add_interest(0.03,5)
这个代码没有任何错误,我想问一下为什么下面那个子类继承了上面的父类,但是没有写出继承属性的代码如BankAccount.__init__(self,name,account),却可以继承父类属性中的name和account而self.balance却不归零? 展开
def __init__(self,name,account):
self.name=name
self.account=account
self.balance=0.0
print 'Welcome!',name,account
def displaybalance(self):
print 'Your balance is',self.balance
def save(self,money):
self.balance=self.balance+money
print 'You have save',money,'to your account.'
self.displaybalance()
def take(self,money):
if self.balance>=money:
self.balance=self.balance-money
print 'You have take',money,'from your account.'
self.displaybalance()
else:
print 'You will take '+str(money)+',but your account has not enough money to take.'
print 'Your account only has',self.balance
class InterestAccount(BankAccount):
def add_interest(self,interest_rate,time):
for i in range(time):
interest=self.balance*interest_rate
self.balance=self.balance+interest
print 'You have saved for',time,'years.'
print 'Add interest to your account:'+'('+str(self.account)+')',interest_rate*100,'percent'
print 'Now your balance is:',self.balance
YQaccount=InterestAccount('YQ',18695997416)
YQaccount.save(450000)
YQaccount.add_interest(0.03,5)
YQaccount.take(200000)
YQaccount.add_interest(0.03,5)
这个代码没有任何错误,我想问一下为什么下面那个子类继承了上面的父类,但是没有写出继承属性的代码如BankAccount.__init__(self,name,account),却可以继承父类属性中的name和account而self.balance却不归零? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询