Class python31
# 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 # #self.courses=[] # # if 'courses' in self.__dict__: #'course' in egon.__dict__ # for obj in self.courses: # obj.tell_course() # # 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, 'female', 300000, -1) # # Birth = Date(alex.name,1900, 13, 43) # # alex.birth = Birth # 组合:alex有生日 # # 组合的应用 # alex.tell_info() # alex.walk() # print(alex.birth) #alex.birth.tell_birth() # # # # class Student: # def __init__(self, name, age, sex, group): # self.name = name # self.age = age # self.sex = sex # # self.group=group # # self.courses = [] # # def walk(self): # print('%s is walking' % self.name) # # def study(self): # print('%s is study' % 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)) # xiaohuang=Student('xh',73,'male','group8') # # Birth=Date(xiaohuang.name,1899,2,29) # # xiaohuang.birth=Birth #组合应用 # xiaohuang.tell_info() # xiaohuang.birth.tell_birth() # # 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, 'female', 300000, -1) # xiaohuang=Student('xh',73,'male','group8') # python=Course('Python',15800,'6mons') # linux=Course('Linux',1000,'3mons') # go=Course('Go',1000,'4mons') # # alex.courses=[] # alex.courses.append(python) # alex.courses.append(go) # # # xiaohuang.courses=[] # xiaohuang.courses.append(python) # # #老师 # print(alex.courses) # alex.courses[0].tell_course() # alex.courses[1].tell_course() # for obj in alex.courses: # obj.tell_course() # # # alex.tell_course() # egon = Teacher('egon', 18, 'male', 300000, 10) # # egon.courses=[] # egon.courses.append(linux) # egon.courses.append(python) # egon.tell_course() #学习 # print(xiaohuang.courses) #改进 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)) 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)) class People: def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex self.courses = [] def walk(self): print('%s is walking' % self.name) def tell_course(self): #self=egon for obj in self.courses: obj.tell_course() class Teacher(People): def __init__(self, name, age, sex, salary, level): People.__init__(self,name,age,sex) self.salary = salary self.level = level 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(People): def __init__(self, name, age, sex, group): People.__init__(self,name,age,sex) self.group=group def study(self): print('%s is study' % 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)) alex = Teacher('alex', 84, 'female', 300000, -1) xiaohuang=Student('xh',73,'male','group8') python=Course('Python',15800,'6mons') linux=Course('Linux',1000,'3mons') go=Course('Go',1000,'4mons') alex.courses=[] alex.courses.append(python) alex.courses.append(go) xiaohuang.courses=[] xiaohuang.courses.append(python) #老师 # print(alex.courses) # alex.courses[0].tell_course() # alex.courses[1].tell_course() # for obj in alex.courses: # obj.tell_course() # alex.tell_course() # egon = Teacher('egon', 18, 'male', 300000, 10) # egon.courses=[] # egon.courses.append(linux) # egon.courses.append(python) # egon.tell_course() #学习 # print(xiaohuang.courses) #序列化 # class Student: # def __init__(self, name, age, sex, group): # self.name=name # self.age=age # self.sex=sex # self.group=group # # def study(self): # print('%s is study' % 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)) # import time # time.sleep(10000) # username=input('user: ') # age=int(input('age: ')) # sex=input('sex: ') # group=input('group: ') # xiaohuang=Student(username,age,sex,group) # xiaohuang.tell_info() # xiaohuang=Student('xh',18,'male','group1') import pickle # res=pickle.dumps(xiaohuang) # print(res) # # with open('studentdb.pkl','wb') as f: # f.write(res) # # with open('studentdb.pkl','rb') as f: # res=f.read() # obj=pickle.loads(res) # print(obj) # obj.tell_info() #简洁的写法 import pickle # xiaohuang=Student('xh',18,'male','group1') # 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()