# -*- coding:utf-8 -*-#! python3class dvd: def __init__(self,name,price,state): self.name=name; self.price=price self.state=state def __str__(self): stat='已借出'; if self.state==1: stat='未借出' return '名称:《%s》 单价:%d 状态:%s'%(self.name,self.price,stat)class DVDManager: #存储 多个dvds 每一个元素 都是一个DVD dvds=[] def init(self): self.dvds.append(dvd('光辉岁月',10,0)); self.dvds.append(dvd('绅士', 5, 1)); self.dvds.append(dvd('海阔天空', 11, 1)); def Menu(self): self.init() while(True): print('1.查询所有DVD') print('2.增加DVD') print('3.借出DVD') print('4.归还DVD') print('5.退出') choose=int(input('请选择:')); if choose==1: self.showAllDVD(); elif choose==2: self.insertDVD(); elif choose==3: self.jiechuDVD(); elif choose==4: self.rollBackDVD(); elif choose==5: print('5.谢谢使用') break; def showAllDVD(self): for d in self.dvds: print(str(d)) def jiechuDVD(self): name = input('请输入DVD的名称:') ret = self.checkDVD(name); if ret != None: if ret.state==0: print('dvd 已经借出去了') #return; else: days=int(input('请输入借出天数:')) ret.state = 0; print('借出 %d 天,应还利息%d '%(days,ret.price*days)) else: print('DVD 不存在!') def rollBackDVD(self): name = input('请输入归还DVD的名称:') ret = self.checkDVD(name); if ret == None: print('DVD 不存在!') else: if ret.state == 1: print('dvd 未借出!') else: days= int(input('请输入借出去了多少天?')) y_money=ret.price * days print('借出 %d 天,应还利息%d ' % (days, y_money)) money=0; while True: money= int(input('请输入金额:')) #带学生完成.. if money<y_money: print('不够!') else: break; if money>=y_money: print('找零:%d'%(money-y_money)); ret.state=1; print('归还成功!') def insertDVD(self): name = input('请输入DVD的名称:') ret= self.checkDVD(name); if ret != None: print('DVD 已经存在!') else: price=int(input('请输入价格:')) new_dvd=dvd(name,price,1); self.dvds.append(new_dvd) print('存放成功!') def checkDVD(self,name): for d in self.dvds: if d.name==name: return d;#返回当前对象-->内存地址 else: return None;manager= DVDManager();manager.Menu();
# -*- coding:utf-8 -*-#! python3class pet: def kanbing(self): print('给宠物看病。。')class Car(pet): def kanbing(self): print('给猫咪看病 吃药,吃小黄鱼..')class Dog(pet): def kanbing(self): print('给狗狗看病 输液 吃骨头....')class bird(pet): def kanbing(self): print('看小鸟看病 拔罐 吃虫子... ')class pig(pet): def kanbing(self): print('给小猪看病 针灸 吃猪粮... ')class ShouVet: def JiuZhen(self,pet): print('兽医出诊..给宠物看病...') pet.kanbing();class person: def kanbing(self): print('给人类看病 望闻问切 多喝水..')dingdang=Car();kala=Dog();huangli=bird();peiqi=pig()bianque=person()syi=ShouVet();syi.JiuZhen(dingdang);syi.JiuZhen(kala);syi.JiuZhen(huangli);syi.JiuZhen(peiqi);syi.JiuZhen(bianque)
# -*- coding:utf-8 -*-#! python3class Operation(object): ''' 四则运算的父类,接收用户输入的数值 ''' def __init__(self, number1=0, number2=0): self.num1 = number1 self.num2 = number2 def GetResult(self): pass pass#加法运算类class OperationAdd(Operation): def GetResult(self): return self.num1 + self.num2#减法运算类class OperationSub(Operation): def GetResult(self): return self.num1 - self.num2#乘法运算类class OperationMul(Operation): def GetResult(self): return self.num1 * self.num2#除法运算类class OperationDiv(Operation): def GetResult(self): if self.num2 == 0: return '除数不能为0 ' return 1.0*self.num1 / self.num2#其他操作符类class OperationUndef(Operation): def GetResult(self): return '操作符错误'#简单工厂类class OperationFactory(object): def choose_oper(self,ch): if ch == '+': return OperationAdd() elif ch == '-': return OperationSub() elif ch == '*': return OperationMul() elif ch == '/': return OperationDiv() else: return OperationUndef()if __name__ == "__main__": ch = '' while not ch == 'q': num1 = input('请输入第一个数值: ') oper = str(input('请输入一个四则运算符: ')) num2 = input('请输入第二个数值: ') # Operation(num1,num2) OF = OperationFactory() oper_obj = OF.choose_oper(oper) oper_obj.num1 = num1 oper_obj.num2 = num2 print('运算结果为: ',oper_obj.GetResult())