Python--面向对象的程序设计之组合应用、开发软件规范
组合应用:
class Teacher: def __init__(self,name,age,sex,salary,level): self.name=name self.age=age self.sex=sex self.salary=salary self.level=level def walk(self): print('%s is walking'%self.name) def teach(self): print('%s is teaching'%self.name) def tell_info(self): print(''' ----------%s info-------- NAME:%s AGE:%s SEX:%s SAL:%s LEVEL:%s '''%(self.name,self.name,self.age,self.sex,self.salary,self.level)) class Date: def __init__(self,year,mon,day): self.year=year self.mon=mon self.day=day def tell_birth(self): print('%s-%s-%s'%(self.year,self.mon,self.day))
alex=Teacher('alex',84,'male',30000,-1)
Birth=Date(1900,13,43)
alex.birth=Birth
alex.tell_info()
print(alex.birth) #<__main__.Date object at 0x00000000027B2860>
alex.birth.tell_birth()
运行结果:
----------alex info--------
NAME:alex
AGE:84
SEX:male
SAL:30000
LEVEL:-1
<__main__.Date object at 0x00000000027B2860>
1900-13-43
改动Date类,表明是谁的生日:
class Date: def __init__(self,name,year,mon,day): self.name=name self.year=year self.mon=mon self.day=day def tell_birth(self): print('%s:%s-%s-%s'%(self.name,self.year,self.mon,self.day)) alex=Teacher('alex',84,'male',30000,-1) Birth=Date(alex.name,1900,13,43) alex.birth=Birth alex.birth.tell_birth() 运行结果: alex:1900-13-43
组合的应用:
class Teacher: def __init__(self,name,age,sex,salary,level): self.name=name self.age=age self.sex=sex self.salary=salary self.level=level def walk(self): print('%s is walking'%self.name) def teach(self): print('%s is teaching'%self.name) def tell_info(self): print(''' ----------%s info-------- NAME:%s AGE:%s SEX:%s SAL:%s LEVEL:%s '''%(self.name,self.name,self.age,self.sex,self.salary,self.level)) class Student: def __init__(self,name,age,sex,group): self.name=name self.age=age self.sex=sex self.group=group def walk(self): print('%s is walking'%self.name) def study(self): print('%s is studying'%self.name) def tell_info(self): print(""" -------%s info------- NAME:%s AGE:%s SEX:%s GROUP:%s """%(self.name,self.name,self.age,self.sex,self.group)) class Course: def __init__(self,name,price,period): self.name=name self.price=price self.period=period def tell_course(self): print(''' ------%s info------ course name:%s course price:%s course period:%s '''%(self.name,self.name,self.price,self.period)) alex=Teacher('alex',84,'male',30000,-1) xiaohuang=Student('xiaohuang',73,'male','group8') python=Course('Python',15800,'6 mons') linux=Course('Linux',12800,'3 mons') go=Course('Go',9800,'4 mons') alex.courses=[] alex.courses.append(python) alex.courses.append(go) print(alex.courses) #[<__main__.Course object at 0x0000000001EAA128>, <__main__.Course object at 0x0000000001E98CC0>] xiaohuang.courses=[] xiaohuang.courses.append(python) print(xiaohuang.courses) #[<__main__.Course object at 0x0000000001EAA128>] 运行结果: [<__main__.Course object at 0x0000000001EAA128>, <__main__.Course object at 0x0000000001E98CC0>] [<__main__.Course object at 0x0000000001EAA128>]
进一步优化,可以查看对象的课程:
class Teacher: def __init__(self,name,age,sex,salary,level): self.name=name self.age=age self.sex=sex self.salary=salary self.level=level self.courses = [] def walk(self): print('%s is walking'%self.name) def teach(self): print('%s is teaching'%self.name) def tell_info(self): print(''' ----------%s info-------- NAME:%s AGE:%s SEX:%s SAL:%s LEVEL:%s '''%(self.name,self.name,self.age,self.sex,self.salary,self.level)) def tell_course(self): #self=egon if 'courses' in self.__dict__: #'courses' in egon.__dict__ for obj in self.courses: obj.tell_course() class Course: def __init__(self,name,price,period): self.name=name self.price=price self.period=period def tell_course(self): print(''' ------%s info------ course name:%s course price:%s course period:%s '''%(self.name,self.name,self.price,self.period)) egon=Teacher('egon',18,'male',30000,10) python=Course('Python',15800,'6 mons') linux=Course('Linux',12800,'3 mons') go=Course('Go',9800,'4 mons') egon.courses.append(python) egon.courses.append(go) egon.tell_course()
我们需要注意一点:
序列化:
import pickle xiaohuang=Student('xiaohuang',73,'male','group8') python=Course('Python',15800,'6 mons') linux=Course('Linux',12800,'3 mons') go=Course('Go',9800,'4 mons') xiaohuang.courses.append(python) xiaohuang.courses.append(go) res=pickle.dumps(xiaohuang) print(res) with open('studentdb.pkl','wb') as f: f.write(res) 运行结果: b'\x80\x03c__main__\nStudent\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\t\x00\x00\x00xiaohuangq\x04X\x03\x00\x00\x00ageq\x05KIX\x03\x00\x00\x00sexq\x06X\x04\x00\x00\x00maleq\x07X\x05\x00\x00\x00groupq\x08X\x06\x00\x00\x00group8q\tX\x07\x00\x00\x00coursesq\n]q\x0b(c__main__\nCourse\nq\x0c)\x81q\r}q\x0e(h\x03X\x06\x00\x00\x00Pythonq\x0fX\x05\x00\x00\x00priceq\x10M\xb8=X\x06\x00\x00\x00periodq\x11X\x06\x00\x00\x006 monsq\x12ubh\x0c)\x81q\x13}q\x14(h\x03X\x02\x00\x00\x00Goq\x15h\x10MH&h\x11X\x06\x00\x00\x004 monsq\x16ubeub.'
注意:json不能序列化对象,我们应该用pickle
反序列化:
with open('studentdb.pkl','rb') as f: res=f.read() obj=pickle.loads(res) print(obj) obj.tell_info() 运行结果: ------Python info------ course name:Python course price:15800 course period:6 mons ------Go info------ course name:Go course price:9800 course period:4 mons
反序列化一定要基于类在内存里面
实现与用户交互:
class Student: def __init__(self,name,age,sex,group): self.name=name self.age=age self.sex=sex self.group=group self.courses = [] def study(self): print('%s is studying'%self.name) def tell_info(self): print(""" -------%s info------- NAME:%s AGE:%s SEX:%s GROUP:%s """%(self.name,self.name,self.age,self.sex,self.group)) def tell_course(self): if 'courses' in self.__dict__: for obj in self.courses: obj.tell_course() name=input('user: ').strip() age=int(input('age: ').strip()) sex=input('sex: ').strip() group=input('group: ').strip() xiaohuang=Student(name,age,sex,group) xiaohuang.tell_info() 运行结果: user: xiaohuang age: 73 sex: male group: group8 -------xiaohuang info------- NAME:xiaohuang AGE:73 SEX:male GROUP:group8
简洁的写法:
class Student: def __init__(self,name,age,sex,group): self.name=name self.age=age self.sex=sex self.group=group self.courses = [] def study(self): print('%s is studying'%self.name) def tell_info(self): print(""" -------%s info------- NAME:%s AGE:%s SEX:%s GROUP:%s """%(self.name,self.name,self.age,self.sex,self.group)) def tell_course(self): if 'courses' in self.__dict__: for obj in self.courses: obj.tell_course() class Course: def __init__(self, name, price, period): self.name = name self.price = price self.period = period def tell_course(self): print(''' ------%s info------ course name:%s course price:%s course period:%s ''' % (self.name, self.name, self.price, self.period)) import pickle xiaohuang=Student('xiaohuang',18,'male','group8') with open('studentdb.pkl','wb') as f: pickle.dump(xiaohuang,f) with open('studentdb.pkl','rb') as f: obj=pickle.load(f) obj.tell_info() 运行结果: -------xiaohuang info------- NAME:xiaohuang AGE:18 SEX:male GROUP:group8
模块的运用:
先定义一下几个文件:
各文件内容如下:
models.py:
class Student: def __init__(self,name,age,sex,group): self.name=name self.age=age self.sex=sex self.group=group self.courses = [] def study(self): print('%s is studying'%self.name) def tell_info(self): print(""" -------%s info------- NAME:%s AGE:%s SEX:%s GROUP:%s """%(self.name,self.name,self.age,self.sex,self.group))
pickle_obj.py:
import pickle def get_obj(): with open('studentdb.pkl', 'rb') as f: res=f.read() obj=pickle.loads(res) obj.tell_info() with open('studentdb.pkl', 'rb') as f: obj = pickle.load(f) obj.tell_info()
start.py:
from models import Student
import pickle_obj
pickle_obj.get_obj()
还有我们事先准备好的studentdb.pkl文件。
运行start.py文件,效果如下:
-------xiaohuang info------- NAME:xiaohuang AGE:18 SEX:male GROUP:group8
接下来看开发软件的规范:
bin文件夹一般放可执行文件,conf文件夹一般放可配置文件,db文件夹一般方法数据文件,还有log目录,lib目录(一般存放常用小工具),src目录放核心,类,逻辑等
开发选课系统:
main函数提供菜单,用户可以选择
今天作业:老师,学生,课程三个类实现选课系统