废话不多说,直接上代码
class People:
school = 'liffycity'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class Teacher(People):
def __init__(self, name, age, sex, level, salary):
super().__init__(name, age, sex)
self.level = level
self.salary = salary
def teacher(self):
print('%s is teaching' % self.name)
class Student(People):
def __init__(self, name, age, sex, class_time):
super().__init__(name, age, sex)
self.class_time = class_time
def learn(self):
print('%s is learning' % self.name)
class Course:
def __init__(self, course_name, course_price, course_period):
self.course_name = course_name
self.course_price = course_price
self.course_period = course_period
def tell_info(self):
print('课程名<%s> 课程价钱<%s> 课程周期<%s>' % (self.course_name, self.course_price, self.course_period))
class Date:
def __init__(self, year, mon, day):
self.year = year
self.mon = mon
self.day = day
def tell_info(self):
print('%s-%s-%s' % (self.year, self.mon, self.day))
teacher1 = Teacher('winnie', 18, 'female', 10, 300)
teacher2 = Teacher('allen', 18, 'male', 30, 300)
python = Course('python', 3000, '3mons')
linux = Course('linux', 2000, '4mons')
# 组合使用(老师教什么课)
teacher1.course = python # teacher1教python课程,给teacher1创建一个属性course,将python的课程直接赋值给teacher1的course这个属性
teacher2.course = linux # teacher1教python课程,给teacher1创建一个属性course,将python的课程直接赋值给teacher1的course这个属性
print(teacher1.course.course_name) # python
print(teacher2.course.course_name) # linux
teacher1.course.tell_info() # 课程名<python> 课程价钱<3000> 课程周期<3mons>
# 组合使用(学生上什么课)
print('==========================')
student1 = Student('vivian', 28, 'female', '08:30:00')
student1.course1 = python
student1.course2 = linux
student1.course1.tell_info()
student1.course2.tell_info()
student1.courses = []
student1.courses.append(python)
student1.courses.append(linux)
# 组合使用(学生的生日、学生的课程)
print('==========================')
student1 = Student('vivian', 28, 'female', '08:30:00')
d = Date(1991, 12, 2)
python = Course('pyhton', 3000, '3mons')
student1.birth = d
student1.birth.tell_info()
student1.courses = python
student1.courses.tell_info()
结果:
python
linux
课程名<python> 课程价钱<3000> 课程周期<3mons>
==========================
课程名<python> 课程价钱<3000> 课程周期<3mons>
课程名<linux> 课程价钱<2000> 课程周期<4mons>
==========================
1991-12-2
课程名<pyhton> 课程价钱<3000> 课程周期<3mons>