题目练习笔记
题目一:

import pickle import time import os class Byes: def save(self): avv = time.strftime('%Y-%m-%d %X', time.localtime()).replace(':', '-')#文件命不能使用冒号: a = os.getcwd() + '/'#详细地址少了一个/ 所以给她字符串拼接一下 ab = os.path.join(a, avv) #os模块的路径拼接 with open(ab,"wb")as f:#类写入文件 用pickle模块 写wb pickle.dump(self,f) f.close() with open(ab,'rb')as t: #读 rb ak = pickle.load(t) print(ak.__dict__) t.close() class School(Byes): '这是一个学校类' def __init__(self,name,class_name): self.name = name self.class_name = class_name school_1 = School("北大","一班") school_2 = School("复旦","二班") class Teacher(Byes): '这是一个老师类' def __init__(self,name,school): self.name = name self.school = school teacher_1 = Teacher("张三",school_1.name) teacher_2 = Teacher("李四",school_1.name) teacher_3 = Teacher("王五",school_2.name) class Course(Byes): '这是一个课程类' def __init__(self,name,school,teacher,cycle,cost): self.name = name self.school = school self.teacher = teacher self.cycle = cycle self.cost = cost coures_1 = Course("linux",school_1.name,teacher_1.name,"一个月","一千元") coures_2 = Course("python",school_1.name,teacher_1.name,"两个月","两千元") coures_3 = Course("go",school_2.name,teacher_2.name,"三个月","三千元") coures_1.save() ##后面是实现用户交互 用户选择课程 自动弹出详细信息 并且生成一个文件

浙公网安备 33010602011771号