版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【任务6-7】完整定义与使用1个父类Commodity和2个子类(Book、Handset)【任务描述】(1)在项目Unit06创建Python程序文件6-7.py。(2)在程序文件6-7.py中完整定义与使用1个父类Commodity和2个子类(Book、Handset)。(3)综合应用类的属性与方法计算与输出应付金额、返现金额、优惠金额、运费和实付金额。【任务实施】在PyCharm项目Unit06中创建Python程序文件6-7.py。在程序文件6-7.py中编写程序代码,实现所需功能。编写定义父类Commodity及其多个属性、方法的代码。【任务实施】classCommodity:#定义父类的构造方法
def__init__(self,code="",name="",price=0.0,quantity=0):modityCode=codemodityName=namemodityPrice=pricemodityQuantity=quantity【任务实施】
#定义父类的实例属性
@propertydefcode(self):returnself._code@code.setterdefcode(self,value):self._code=value【任务实施】
@propertydefname(self):returnself._name@name.setterdefname(self,value):self._name=value【任务实施】
@propertydefprice(self):returnself._price@price.setterdefprice(self,value):self._price=value【任务实施】
@propertydefquantity(self):returnself._quantity@quantity.setterdefquantity(self,value):ifnotisinstance(value,int):raiseValueError('购买数量应为正整数!')self._quantity=value【任务实施】
@propertydefcalculateAmount(self):amount=modityPrice*modityNumberreturnamount@propertydefamount(self):amount=self.price*self.numberreturnamount【任务实施】defprintInfo(self,strInfo):print("{:^50s}".format(strInfo))defprintField(self,endMark="\n"):print("{:^9s}".format("商品编号"),end="")print("{:^20s}".format("商品名称"),end="")print("{:^12s}".format("价格"),end=endMark)【任务实施】defprintData(self,endMark="\n"):print("{:^10s}".format(self.code),end="")print("{:^21s}".format(),end="")print("{:^8.2f}".format(self.price),end=endMark)【任务实施】编写定义子类Book及其多个属性、方法的代码。#定义第1个子类BookclassBook(Commodity):def__init__(self,code="",name="",price=0.0,publisher="",editionOrder=1):Commodity.__init__(self,code,name,price)self.bookPublisher=publisherself.bookEdition=editionOrder【任务实施】
#定义子类Book的属性
@propertydefpublisher(self):returnself._publisher@publisher.setterdefpublisher(self,value):self._publisher=value【任务实施】
@propertydefedition(self):returnself._edition@edition.setterdefedition(self,value):self._edition=value【任务实施】defprintField(self):Commodity.printField(self,"")print("{:^7s}".format("出版社名称"),end="")print("{:^16s}".format("版次"))defprintData(self):Commodity.printData(self,"")print("{:^10s}".format(self.publisher),end="")print("{:^6d}".format(self.edition))【任务实施】
@classmethoddefgetDiscountPrice(cls,rank,price):ifrank=="PLUS":discountPrice=price*0.88else:ifrank=="FAN":discountPrice=price*0.90else:discountPrice=price*0.92returndiscountPrice【任务实施】@classmethoddefgetDiscount(cls,number,price):originalTotal=number*pricediscount=0iforiginalTotal>=299:discount=15.00returndiscount【任务实施】@classmethoddefgetCashback(cls,number,price):originalTotal=number*pricereduction=int(originalTotal/100)ifreduction>0:cashback=reduction*50returncashback【任务实施】@classmethoddefgetCarriage(cls,payable):#订单金额<49,收取基础运费6元;订单金额≥49,收取基础运费0元
ifpayable<49:carriage=6.00else:carriage=0.00returncarriage【任务实施】
#输出结算数据
@classmethoddefprintSettlementData(cls,*data):print("应付金额:¥"+"{:.2f}".format(data[0]))print("运费:¥"+"{:.2f}".format(data[1]))print("返现金额:-¥"+"{:.2f}".format(data[2]))print("优惠金额:-¥"+"{:.2f}".format(data[3]))print("实付金额:¥"+"{:.2f}".format(data[4]))【任务实施】编写子类Handset及其多个属性、方法的代码。#定义第2个子类HandsetclassHandset(Commodity):def__init__(self,code="",name="",price=0.0,resolution="",screenSize=""):#在子类中使用supper()函数调用父类的__init__()方法super().__init__(self,code,name,price)self.handsetResolution=resolutionself.handsetScreenSize=screenSize【任务实施】
#定义子类Handset的属性
@propertydefresolution(self):returnself._resolution@resolution.setterdefresolution(self,value):self._resolution=value【任务实施】
@propertydefscreenSize(self):returnself._screenSize@screenSize.setterdefscreenSize(self,value):self._screenSize=value【任务实施】
defprintField(self):Commodity.printField(self,"")print("{:^6s}".format("屏幕尺寸"),end="")print("{:^16s}".format("物理分辨率"))【任务实施】defprintData(self):Commodity.printData(self,"")print("{:^20s}".format(self.screenSize),end="")print("{:^12s}".format(self.resolution))【任务实施】针对创建的类Commodity和两个子类Book、Handset实施以下各项操作。1.创建子类Book的实例对象book代码如下:book=Book()【任务实施】2.通过类的实例对象book给类的属性赋值代码如下:book.code="12563157"="给Python点颜色青少年学编程"book.price=59.80book.quantity=1book.publisher="人民邮电出版社"book.edition=1【任务实施】3.通过类的实例对象book以模拟表格方式输出图书数据代码如下:book.printField()book.printData()运行结果为:商品编号商品名称价格出版社名称版次12563157给Python点颜色青少年学编程59.80人民邮电出版社1【任务实施】4.通过类的实例对象book分别设置父类实例属性的值和用户等级代码如下:book.quantity=5rank="Ordinaryusers"【任务实施】5.调用类的多个方法分别计算应付金额、返现金额、优惠金额、运费和实付金额代码如下:discountPrice=book.getDiscountPrice(rank,book.price)discountAmount=book.quantity*discountPricediscount=book.getDiscount(book.quantity,book.price)cashback=book.getCashback(book.quantity,book.price)discountTotal=discount+cashbackpayable=discountAmount-discountTotalcarriage=book.getCarriage(payable)payable+=carriage【任务实施】6.调用子类book的方法printSettl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年关于库尔勒梨城建设有限公司公开选聘副总经理的备考题库及答案详解1套
- 2026年山西电机制造有限公司招聘备考题库带答案详解
- 2026年中国旅游集团岗位招聘备考题库及参考答案详解
- 2026年中建西部建设新材料科技有限公司招聘备考题库及完整答案详解1套
- 2026年东莞证券股份有限公司河源分公司招聘备考题库含答案详解
- 2026年宁波市鄞州区公立学校招聘编外员工备考题库及完整答案详解1套
- 2026年南昌华路建设咨询监理有限公司招聘备考题库及答案详解1套
- 2026年北京市海淀区富力桃园幼儿园招聘备考题库及参考答案详解一套
- 2026年岭南国防教育基地备考题库技术员招聘备考题库及完整答案详解一套
- 2026年四会市建筑安装工程有限公司公开招聘工作人员备考题库附答案详解
- 安全评估培训体会课件
- 生产安全操作安全培训模板
- 课题班级自主管理申报书
- 国际货运代理公司合伙协议书
- 质量安全环保保证协议书
- 北京市朝阳区2023-2024学年七年级上学期期末质量监测历史试卷及答案
- 教代会提案工作培训指南
- 飞行营地建设项目可行性研究报告
- 2025年副高卫生职称-临床医学检验学技术-临床医学检验临床化学技术(副高)代码:058历年参考题库典型考点含答案解析
- 电大专科水利水电工程水法规与行政执法试题及答案
- 2025年四川单招试题及答案普高
评论
0/150
提交评论