中心主题 £神魔★判官ぃ 2023-06-02 13:55 6阅读 0赞 ### 9.面向对象编程 ### * 步骤 * 面向对象分析OOA * 面向对象设计OOD * 面向对象编程OOP * 实现 * 1.分析对象特征行为 * 2.写类描述对象模块 * 3.实例化,模拟过程 * 特征 * 封装 * 实例1 * def search\_book(title): print(‘您要找的书籍名称:\{\}’.format(title)) book=\{ ‘title’:‘python入门’, ‘price’:80, ‘authon’:‘Peter’, ‘search\_book’:search\_book \} print(book\[‘title’\]) print(book.get(‘price’),0.0) book.get(‘search\_book’)(‘python中级’) - 实例2 - import datetime class Book: def **init**(self, title, price=0.0, author=’’, publisher=None, pudate=datetime.date.today() ): self.title=title, self.price=price self.author=author, self.publisher=publisher, self.pudate=pudate def print_info(self): print("当前这本书的信息如下:") print("这本书的标题:{}".format(self.title)) print("这本书的价格:{}".format(self.price)) print("这本书的作者:{}".format(self.author)) print("这本书的出版社:{}".format(self.publisher)) print("这本书的出版日期:{}".format(self.pudate)) book1=Book(‘c\#经典’,30,‘Tom’,‘北京大学出版社’,datetime.date(2019,10,1)) book1.print\_info() book2=Book(‘Flask’) book2.price=90 book2.print\_info() # print(book1.title,book1.price,sep=’,’) # - 实例3 - import datetime class Book: def **init**(self, title, price=0.0, author=’’, publisher=None, pudate=datetime.date.today() ): self.title=title, self.price=price self.author=author, self.publisher=publisher, self.pudate=pudate def print_info(self): print("当前这本书的信息如下:") print("这本书的标题:{}".format(self.title)) print("这本书的价格:{}".format(self.price)) print("这本书的作者:{}".format(self.author)) print("这本书的出版社:{}".format(self.publisher)) print("这本书的出版日期:{}".format(self.pudate)) def __repr__(self): return '<图书 {} at {}>'.format(self.title,id(self)) book1=Book(‘c\#经典’,30,‘Tom’,‘北京大学出版社’,datetime.date(2019,10,1)) book1.print\_info() book2=Book(‘Flask’) book2.price=90 book2.print\_info() # print(book1.title,book1.price,sep=’,’) # - 属性 - import datetime class student: def **init**(self,name,birthday): self.name=name self.birthday=birthday @property def age(self): return datetime.date.today().year-self.birthday.year @age.setter def age(self,value): raise AttributeError('禁止年龄赋值!') @age.deleter def age(self): raise AttributeError('禁止修改年龄!') if **name**==“**main**”: s=student(‘小代’,datetime.date(1994,6,1)) print(s.name,s.birthday,s.age,sep=’,’) del(s.age) - 继承 - import datetime class Employee: def **init**(self,name,birthday,department,salary): self.name=name self.birthday=birthday self.department=department self.salary=salary def __repr__(self): return '<员工:{}>'.format(self.name) @property def age(self): return datetime.date.today().year-self.birthday.year def give_rise(self,percent,bonus=0.0): self.salary=self.salary*(1+percent+bonus) def working(self): print('员工{}在工作......').format(self.name) class Programer(Employee): def **init**(self,name,birthday,department,salary,major,project): super().**init**(name,birthday,department,salary) self.major=major self.project=project def working(self): print('程序员{}正在开发项目{}.....'.format(self.name,self.project)) if **name**==‘**main**’: p=Programer(‘小代’,datetime.date(1994,6,1),‘技术部’,10000,‘python’,‘crm’) print§ print(p.salary) p.give\_rise(0.2,0.1) print(p.salary) p.working() print(p.age) \- import datetime class Employee: def **init**(self,name,birthday,department,salary): self.name=name self.birthday=birthday self.department=department self.salary=salary def __repr__(self): return '<员工:{}>'.format(self.name) @property def age(self): return datetime.date.today().year-self.birthday.year def give_rise(self,percent,bonus=0.0): self.salary=self.salary*(1+percent+bonus) def working(self): print('员工{}在工作......').format(self.name) class HR(Employee): def **init**(self,name,birthday,department,salary,qualification\_level): Employee.**init**(self,name,birthday,department,salary) self.qualification\_level=qualification\_level def working(self): print('人事{}正在招聘新员工......'.format(self.name)) class Programer(Employee): def **init**(self,name,birthday,department,salary,major,project): super().**init**(name,birthday,department,salary) self.major=major self.project=project def working(self): print('程序员{}正在开发项目{}.....'.format(self.name,self.project)) if **name**==‘**main**’: p=Programer(‘小代’,datetime.date(1994,6,1),‘技术部’,10000,‘python’,‘crm’) print§ print(p.salary) p.give\_rise(0.2,0.1) print(p.salary) p.working() print(p.age) h=HR(‘建邦’,datetime.date(1993,7,24),‘人事部’,30000,qualification\_level=10) print(h) print(h.name,h.salary,h.qualification\_level,sep=’//’) - 多态 - import datetime class Employee: def **init**(self,name,birthday,department,salary): self.name=name self.birthday=birthday self.department=department self.salary=salary def __repr__(self): return '<员工:{}>'.format(self.name) @property def age(self): return datetime.date.today().year-self.birthday.year def give_rise(self,percent,bonus=0.0): self.salary=self.salary*(1+percent+bonus) def working(self): print('员工{}在工作......').format(self.name) class HR(Employee): def **init**(self,name,birthday,department,salary,qualification\_level): Employee.**init**(self,name,birthday,department,salary) self.qualification\_level=qualification\_level def working(self): print('人事{}正在招聘新员工......'.format(self.name)) class Programer(Employee): def **init**(self,name,birthday,department,salary,major,project): super().**init**(name,birthday,department,salary) self.major=major self.project=project def working(self): print('程序员{}正在开发项目{}.....'.format(self.name,self.project)) if **name**==‘**main**’: p=Programer(‘小代’,datetime.date(1994,6,1),‘技术部’,10000,‘python’,‘crm’) print§ print(p.salary) p.give\_rise(0.2,0.1) print(p.salary) p.working() print(p.age) h=HR(‘建邦’,datetime.date(1993,7,24),‘人事部’,30000,qualification\_level=10) print(h) print(h.name,h.salary,h.qualification\_level,sep=’//’) *XMind: ZEN - Trial Version*
相关 中心主题 5.数据类型 元组(tuple) 特点 任意对象的有序集合 通过下标来访问 属于‘’不可变类型‘’ ╰+哭是因爲堅強的太久メ/ 2023年08月17日 16:52/ 0 赞/ 110 阅读
相关 中心主题 5.数据类型 数值 声明、赋值、使用 表达式 显示 ‘0:.2f’.format(4.444) import Dear 丶/ 2023年08月17日 16:18/ 0 赞/ 130 阅读
相关 中心主题 4.链表 概念 顺序表,将元素顺序地存放在-块连续的存储区里,元素间的顺序关系由它们的存储顺序自然表示。 链表,将元素存放在通过链接构造起来的一系列存储 谁践踏了优雅/ 2023年06月08日 14:57/ 0 赞/ 133 阅读
相关 中心主题 18.文件读取 txt def write\_txt(): with open(‘data.txt’,‘w’,encoding=‘utf8’) a 淩亂°似流年/ 2023年06月07日 08:23/ 0 赞/ 33 阅读
相关 中心主题 15.正则表达式 概述 概念 Regular Expression 一种文本模式,描述在搜索文本时要匹配的一个或多个字符 分手后的思念是犯贱/ 2023年06月06日 12:22/ 0 赞/ 27 阅读
相关 中心主题 13.对象持久化 扁平文件 文本文件 使用列表存储出现,把标点符号独立存储问题 使用eval()可以解决上述问题,但是当 ゝ一纸荒年。/ 2023年06月05日 13:59/ 0 赞/ 52 阅读
还没有评论,来说两句吧...