先看一段代码:
1 class People: 2 school = 'luffycity' 3 4 def __int__(self, name, age, sex): 5 self.name = name 6 self.age = age 7 self.sex = sex 8 9 10 class Teacher(People): 11 12 def __init__(self, name, age, sex, level, salary): 13 super().__int__(name, age, sex) 14 self.level = level 15 self.salary = salary 16 17 def teach(self): 18 print('%s is teaching' % self.name) 19 20 21 class Student(People): 22 23 def __init__(self, name, age, sex, class_time): 24 super().__int__(name, age, sex) 25 self.class_time = class_time 26 27 def learn(self): 28 print('%s is learning' % self.name) 29 30 31 teacher1 = Teacher('alex', 18, 'male', 10, 3000) 32 student1 = Student('张三', 28, 'female', '08:30:00') 33 print(teacher1.__dict__) 34 print(student1.__dict__) 35 36 结果为: 37 38 {'name': 'alex', 'age': 18, 'sex': 'male', 'level': 10, 'salary': 3000} 39 {'name': '张三', 'age': 28, 'sex': 'female', 'class_time': '08:30:00'}
如果要为,teacher类添加老师所教的课程信息,如课程名,课程价格,课程周期等,可以在init函数里面增加,但是如果多个老师教同一门课程,就会出现代码重复的问题,如下
代码所示:
1 class People: 2 school = 'luffycity' 3 4 def __int__(self, name, age, sex): 5 self.name = name 6 self.age = age 7 self.sex = sex 8 9 10 class Teacher(People): 11 12 def __init__(self, name, age, sex, level, salary, course_name, course_price, cource_period): 13 super().__int__(name, age, sex) 14 self.level = level 15 self.salary = salary 16 self.course_name = course_name 17 self.course_price = course_price 18 self.course_period = cource_period 19 20 def teach(self): 21 print('%s is teaching' % self.name) 22 23 24 class Student(People): 25 26 def __init__(self, name, age, sex, class_time): 27 super().__int__(name, age, sex) 28 self.class_time = class_time 29 30 def learn(self): 31 print('%s is learning' % self.name) 32 33 34 teacher1 = Teacher('alex', 18, 'male', 10, 3000, 'python', 3000, '3mons') 35 teacher2 = Teacher('egon', 28, 'male', 30, 2000, 'python', 3000, '3mons') 36 student1 = Student('张三', 28, 'female', '08:30:00') 37 print(teacher1.__dict__) 38 print(teacher2.__dict__) 39 print(student1.__dict__) 40 41 结果为: 42 43 {'name': 'alex', 'age': 18, 'sex': 'male', 'level': 10, 'salary': 3000, 'course_name': 'python', 'course_price': 3000, 'course_period': '3mons'} 44 {'name': 'egon', 'age': 28, 'sex': 'male', 'level': 30, 'salary': 2000, 'course_name': 'python', 'course_price': 3000, 'course_period': '3mons'} 45 {'name': '张三', 'age': 28, 'sex': 'female', 'class_time': '08:30:00'}
怎样解决代码重复问题,可以新加一个course类:如下所示:
1 class Course: 2 def __init__(self, course_name, course_price, course_period): 3 self.course_name = course_name 4 self.course_price = course_price 5 self.course_period = course_period
但是,course类和teacher类并没有继承关系,而是一种老师有课程的关系,这样就讲到了组合的用处,就是为了teacher这样的类访问course这样的类的方法,实例如下:
1 python = Course('python', 3000, '3mons') 2 3 teacher1.course = python 4 teacher2.course = python 5 6 print(python) 7 print(teacher1.course) 8 print(teacher2.course)
把teacher1的course属性指向一个course实例化对象python,这样执行teacher1.course.就相当于执行python.
结果如下:
1 <__main__.Course object at 0x000001F01BFC3BA8> 2 <__main__.Course object at 0x000001F01BFC3BA8> 3 <__main__.Course object at 0x000001F01BFC3BA8> 4 5 为同一块内存地址
1 print(python.course_name) 2 3 print(teacher1.course.course_name) 4 print(teacher2.course.course_name) 5 6 结果为: 7 8 python 9 python 10 python
为course添加tell_info 方法,
1 def tell_info(self): 2 print('课程名<%s> 课程价钱<%s> 课程周期<%s>' % (self.course_name, self.course_price, self.course_period))
teacher1.course.tell_info()的执行结果为:
课程名<python> 课程价钱<3000> 课程周期<3mons>
增加linux实例化对象:
linux = Course('linux', 2000, '4mons') student1学了两门课程 student1.course1 = python student1.course2 = linux 查询student1学的课程信息
student1.course1.tell_info()
student1.course2.tell_info()
结果为:
课程名<python> 课程价钱<3000> 课程周期<3mons> 课程名<linux> 课程价钱<2000> 课程周期<4mons>
增加一个Date类:
1 class Date: 2 def __init__(self, year, mon, day): 3 self.year = year 4 self.mon = mon 5 self.day = day 6 7 def tell_info(self): 8 print('%s-%s-%s' % (self.year, self.mon, self.day)) 9 10 实例话一个Date对象d 11 d = Date(1988, 4, 20) 12 student1.birth = d 13 student1.birth.tell_info() 14 student1.course1.tell_info() 15 16 结果为:
17 1988-4-20 18 课程名<python> 课程价钱<3000> 课程周期<3mons>