【Python】python-一个class继承的小case
2015-10-28 19:42
656 查看
#-*- coding:utf-8 -*- #定义银行类,包含属性:用户名,账户,余额;包含方法有:查询余额,存钱,取钱 class BankAccount(): def __init__(self,name,account,balance): self.name=name self.account=account self.balance=balance def printBalance(self): print "My balance is ",self.balance def saveMoney(self,saving): self.balance+=saving def drawMoney(self,drawing): self.balance-=drawing #定义利率计算类,继承银行类,新增属性有银行利率,新增方法有计算本息金额 class InterestAccount(BankAccount): def __init__(self,name,account,balance,interest): BankAccount.__init__(self,name,account,balance) self.interest=interest def addInterest(self): self.balance=self.balance*(1+interest) print "After a year,My balance is ",self.balance #自定义输入用户名 账户 余额 利率等信息 name=raw_input("Enter your name :") account=raw_input("Enter your account:") balance=float(raw_input("Enter your balance:")) interest=float(raw_input("Enter the bank interest:")) #创建类InterestAccount的一个对象 myAccount=InterestAccount(name,account,balance,interest) #调用类的方法进行测试 myAccount.printBalance() saving=float(raw_input("Enter your saving:")) myAccount.saveMoney(saving) myAccount.printBalance() drawing=float(raw_input("Enter your drawing:")) myAccount.printBalance() myAccount.addInterest()
相关文章推荐
- [LeetCode]题解(python):034-Search for a Range
- [LeetCode]题解(python):033-Search in Rotated Sorted Array
- [LeetCode]题解(python):032-Longest Valid Parentheses
- python 学习笔记(03)
- python脚本获取本机公网ip
- [LeetCode]题解(python):031-Next Permutation
- python os、multiprocess、 threading
- Python多进程记录日志
- Python *与** 参数问题
- 使用python进行数据转码
- python函数参数带星号*
- (5)python循环语句
- python爬虫学习笔记1——糗百段子爬取
- Python的几种实现
- Python的几种实现
- python多线程机制
- Python操作Excel读写--xlrd、xlwt模块
- Spinbox 实例
- 使用python,批量导入数据到elasticsearch中
- Python-os与shutil对文件和文件夹的操作