标签:info python app end super teacher self 使用 inux
类的使用有两种:
一种继承:描述类与类之间的关系,表示什么是什么的什么?
另一种就是组合:
组合就是一个类,该类拥有的属性是来自另一个类,就叫做类的组合使用;
1 class People: 2 def __init__(self,name,age,sex): 3 self.name = name 4 self.age = age 5 self.sex = sex 6 7 class Teacher(People): 8 9 def __init__(self,name,age,sex,level,sal): 10 super().__init__(name,age,sex) 11 self.levle = level 12 self.sal = sal 13 self.course = [] 14 15 def change_score(self): 16 17 print(‘%s changing to score‘ %self.name) 18 19 20 def teacher_data(self): 21 print(‘老师%s课程信息如下:‘ %(self.name).center(50,‘=‘)) 22 for course in self.course: 23 course.info() 24 class student(People): 25 def __init__(self,name,age,sex): 26 super().__init__(name,age,sex) 27 self.course = [] 28 29 def choice_courses(self): 30 print(‘%s choicing course...‘ %self.name) 31 32 def student_data(self): 33 print(‘学生%s课程信息如下:‘ %(self.name).center(50,‘=‘)) 34 for course in self.course: 35 course.info() 36 37 class Course: 38 def __init__(self,cname,tras,price): 39 self.cname = cname 40 self.tras = tras 41 self.price = price 42 43 def info(self): 44 print(‘‘‘ 45 课程:%s 46 周期:%s 47 价格:%s 48 49 ‘‘‘ 50 51 %(self.cname,self.tras,self.price)) 52 53 54 t1 = Teacher(‘egon‘,18,‘male‘,10,120000) 55 s1 = student(‘kermit‘,18,‘male‘) 56 python = Course(‘python‘,‘4mon‘,15000) 57 linux = Course(‘linux‘,‘5mon‘,12000) 58 go = Course(‘go‘,‘3mon‘,18000) 59 60 t1.course.append(python) 61 t1.course.append(linux) 62 t1.course.append(go) 63 t1.teacher_data() 64 65 s1.course.append(python) 66 s1.course.append(linux) 67 s1.course.append(go) 68 s1.student_data()
标签:info python app end super teacher self 使用 inux
原文地址:https://www.cnblogs.com/kermitjam/p/8850211.html