2020Python作业——封装2+继承
@2020.4.9
作业一:自己试验一下菱形问题下的属性查找顺序(经典类用Python2试验)
作业二:
在昨天作业的基础之上——周三的作业—类与对象2+封装 https://www.cnblogs.com/bigorangecc/p/12663017.html
(本周周二作业—类与对象—https://www.cnblogs.com/bigorangecc/p/12656565.html)
# 1、引入属性访问控制+property(封装----装饰器)
# 2、引入继承与派生的概念来减少代码冗余 (继承与派生)
注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承(耐嚼的一句话)
【基础版】
# 一、学校---# 校区创建完毕后,可以为每个校区创建班级 # 定义学校类#基础版 class School: school_name = 'OLDBOY' def __init__(self, nickname, addr): self.nickname = nickname self.addr = addr self.classes = [] # 为【学校】关联【班级】 def related_class(self, class_obj): # self.classes.append(班级名字) # self.classes.append(class_name) self.classes.append(class_obj) # 函数功能:打印每个校区的每个班级的课程信息 def tell_class(self): # 改 # 打印的校区的名字 print(self.nickname.center(60,'=')) # 格式化打印效果:==========================校区名称=========================== # 打印班级开设的课程信息 for class_obj in self.classes: class_obj.tell_course() # 实例化学校类 school_obj1=School('老男孩魔都校区','上海') school_obj2=School('老男孩帝都校区','北京') # 二、班级---# 班级创建完毕后,可以为每个班级创建课程 # 定义班级类 class Class: def __init__(self, name): self.name = name self.course = None # 为【班级】关联【课程】 def related_course(self, course_obj): # self.course = course_name self.course = course_obj def tell_course(self): print('%s' % self.name,end=" ") # 打印班级信息 self.course.tell_info() # 打印课程的详细信息 # def related_student(self,student_obj): # self.student = student_obj def tell_class(self): print('%s'%self.name) self.tell_course() # 实例化班级 # 1、创建班级 class_obj1 = Class('脱产14期') class_obj2 = Class('脱产15期') class_obj3 = Class('脱产29期') # 4、为学校开设班级(为【学校】关联【班级】) # 上海校区开了:脱产14期,上海校区开了脱产15期 school_obj1.related_class(class_obj1) school_obj1.related_class(class_obj2) # 北京校区开了:脱产29期 school_obj2.related_class(class_obj3) # 三、课程类 # 定义课程类 class Course: def __init__(self,name,period,price): self.name=name self.period=period self.price=price def tell_info(self): print('<课程名:%s 周期:%s 价钱:%s>' %(self.name,self.period,self.price)) # 实例化课程类 # 1、创建课程 course_obj1=Course('python全栈开发','6mons',20000) course_obj2=Course('linux运维','5mons',18000) # 3、为【班级】关联【课程对象】 class_obj1.related_course(course_obj1) class_obj2.related_course(course_obj2) class_obj3.related_course(course_obj1) # 【查询一】 # 查看每个校区的每个班级的课程信息(先是为【学校】关联【班级】,再为【班级】关联【课程】) # tell_class(tell_course())--- tell_course(tell_info())---tell_info(课程信息) school_obj1.tell_class() school_obj2.tell_class() # 输出结果: # ==========================老男孩魔都校区=========================== # 脱产14期 <课程名:python全栈开发 周期:6mons 价钱:20000> # 脱产15期 <课程名:linux运维 周期:5mons 价钱:18000> # ==========================老男孩帝都校区=========================== # 脱产29期 <课程名:python全栈开发 周期:6mons 价钱:20000> # 四、学生类--# 学生创建完毕后,学生可以选择班级 # 定义学生类 class Student: def __init__(self,s_school,s_name,s_age,s_number,s_gender): self.stu_school = s_school self.stu_name = s_name self.stu_age = s_age self.stu_number = s_number self.stu_gender = s_gender # def choose_class(self,class_obj): # 学生创建完毕后,学生可以选择班级 # self.classes= class_obj def choose_class(self): print('学生%s正在选课程班:'%self.stu_name) # 为【学生】关联【学校】 def related_school(self,school_obj): self.stu_school= school_obj # 为【学生】关联【班级】 def related_class(self,class_obj): self.stu_class= class_obj def tell_stu_message(self): print(('学生 %s 2020年档案表' %(self.stu_name)).center(60, '=')) print('<学校:%s 姓名:%s 年龄:%s 学号:%s 性别:%s>' %(self.stu_school,self.stu_name,self.stu_age,self.stu_number,self.stu_gender)) # self.stu_class.tell_class() self.choose_class() self.stu_class.tell_course() # 实例化学生类 # 1、创建学生 stu_obj1 = Student('老男孩魔都校区','CC',18,2020001,'female') stu_obj2 = Student('老男孩帝都校区','MILI',19,2020007,'male') stu_obj3 = Student('老男孩魔都校区','MELA',20,2020009,'male') # 2、为【学生】关联【班级】 stu_obj1.related_class(class_obj1) stu_obj2.related_class(class_obj2) stu_obj3.related_class(class_obj3) # 【查询二】查询每个学生的学校,班级,课程信息 stu_obj1.tell_stu_message() stu_obj2.tell_stu_message() stu_obj3.tell_stu_message() # 输出结果: # =======================学生 CC 2020年档案表======================= # <学校:老男孩魔都校区 姓名:CC 年龄:18 学号:2020001 性别:female> # 脱产14期 <课程名:python全栈开发 周期:6mons 价钱:20000> # ======================学生 MILI 2020年档案表====================== # <学校:老男孩帝都校区 姓名:MILI 年龄:19 学号:2020007 性别:male> # 脱产15期 <课程名:linux运维 周期:5mons 价钱:18000> # ======================学生 MELA 2020年档案表====================== # <学校:老男孩魔都校区 姓名:MELA 年龄:20 学号:2020009 性别:male> # 脱产29期 <课程名:python全栈开发 周期:6mons 价钱:20000> # 五、老师类---#老师创建完毕后,可以为学生打分 # 定义老师类 class Teacher: def __init__(self,t_name, t_age, salary, level): self.teacher_name = t_name self.teacher_age = t_age self.teacher_salary = salary self.teacher_level = level # def related_student(self,stu_obj): # self.student=stu_obj def score(self,stu_obj): print('教师%s正在为学生%s改分' %(self.teacher_name,stu_obj)) # 实例化老师类 # 1、创建老师对象 tea_obj1 = Teacher('TANK',35,'3800','助教') tea_obj2 = Teacher('EGON',28,'4800','讲师') tea_obj3 = Teacher('LULU',25,'3800','班主任') # # 2、为老师关联学生 # tea_obj1.related_student(stu_obj1) # tea_obj2.related_student(stu_obj2) # tea_obj3.related_student(stu_obj3) # 【查询改分老师】 tea_obj1.score(stu_obj1.stu_name) tea_obj2.score(stu_obj2.stu_name) tea_obj3.score(stu_obj3.stu_name)
【升级版 #2 】
class School: school_name = 'OLDBOY' def __init__(self, nickname, addr): self.nickname = nickname self.addr = addr self.classes = [] def related_class(self, class_obj): self.classes.append(class_obj) def tell_class(self): print(self.nickname.center(60, '=')) # 打印的校区的名字 for class_obj in self.classes: # 打印班级开设的课程信息 class_obj.tell_course() class Class: def __init__(self, name): self.name = name self.course = None def related_course(self, course_obj): self.course = course_obj def tell_course(self): print('%s' % self.name, end=" ") # 打印班级信息 self.course.tell_info() # 打印课程的详细信息 def tell_class(self): print('%s' % self.name) self.tell_course() class Course: def __init__(self, name, period, price): self.name = name self.period = period self.price = price def tell_info(self): print('<课程名:%s 周期:%s 价钱:%s>' % (self.name, self.period, self.price)) class OldboyPeople: school = 'OLDBOY' def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex class Student(OldboyPeople): # stu_obj1 = Student('老男孩魔都校区', 'CC', 18, 2020001, 'female') def __init__(self,stu_name,stu_age,stu_sex,stu_number): OldboyPeople.__init__(self,stu_name,stu_age,stu_sex) self.stu_number=stu_number def choose_course(self): print('学生%s 正在选课' % self.name) class Teacher(OldboyPeople): # 老师的空对象,'egon',18,'male',3000,10 def __init__(self, name, age, sex, salary, level): # 指名道姓地跟父类OldboyPeople去要__init__ OldboyPeople.__init__(self, name, age, sex) self.salary = salary self.level = level def score(self): print('老师 %s 正在给学生打分' % self.name) # 实例化学校类 school_obj1 = School('老男孩魔都校区', '上海') school_obj2 = School('老男孩帝都校区', '北京') # 实例化班级 # 1、创建班级 class_obj1 = Class('脱产14期') class_obj2 = Class('脱产15期') class_obj3 = Class('脱产29期') # 2、为学校开设班级(为【学校】关联【班级】) # 上海校区开了:脱产14期,上海校区开了脱产15期 school_obj1.related_class(class_obj1) school_obj1.related_class(class_obj2) # 北京校区开了:脱产29期 school_obj2.related_class(class_obj3) # 实例化课程类 # 1、创建课程 course_obj1 = Course('python全栈开发', '6mons', 20000) course_obj2 = Course('linux运维', '5mons', 18000) # 2、为【班级】关联【课程对象】 class_obj1.related_course(course_obj1) class_obj2.related_course(course_obj2) class_obj3.related_course(course_obj1) # 【查询一】 # 查看每个校区的每个班级的课程信息(先是为【学校】关联【班级】,再为【班级】关联【课程】) # tell_class(tell_course())--- tell_course(tell_info())---tell_info(课程信息) school_obj1.tell_class() school_obj2.tell_class() # 实例化学生 # 1、创建学生 stu_obj1 = Student('CC',18,'female',2020001) stu_obj2 = Student('MILI',19,'male',2020007) stu_obj3 = Student('MELA',20,'male',2020009) # 2、选课 stu_obj1.choose_course() stu_obj2.choose_course() stu_obj3.choose_course() # 实例化老师 tea_obj = Teacher('egon', 18, 'male', 3000, 10) tea_obj.score() # 输出结果 # ==========================老男孩魔都校区=========================== # 脱产14期 <课程名:python全栈开发 周期:6mons 价钱:20000> # 脱产15期 <课程名:linux运维 周期:5mons 价钱:18000> # ==========================老男孩帝都校区=========================== # 脱产29期 <课程名:python全栈开发 周期:6mons 价钱:20000> # 学生CC 正在选课 # 学生MILI 正在选课 # 学生MELA 正在选课 # 老师 egon 正在给学生打分
🐱不负韶华,只争朝夕🍚