# -*- coding:utf-8 -*-
import time class Person(object):
'''
定义父类:人
属性:姓名,年龄
方法:走路(打印:姓名+“正在走路”)
''' def __init__(self, name, age):
self.name = name
self.age = age def walk(self):
print self.name + "正在走路" class Teacher(Person):
'''
定义子类:老师
属性:上课学生(集合)
方法:授课(打印:姓名+“老师正在上课”, 并调用所有上课学生的听课方法)
下课(打印:“下课”,并调用所有学生的走路方法)
'''
# 方法重载(name), 覆盖父类初始值
def __init__(self,name,age, students):
Person.__init__(self, name, age)
self.students = students def teach(self):
print self.name + "老师正在上课"
for i in self.students:
i.learn() def classover(self):
print "下课"
for i in self.students:
i.walk() class Student(Person):
'''
定义子类:学生
属性:学号
方法:听课(打印:姓名+学号+“正在听课”)
'''
def __init__(self, name,age, studentID):
Person.__init__(self, name, age)
self.studentID = studentID def learn(self):
print self.name + str(self.studentID) + "正在听课" if __name__ == "__main__": # 实例化一个老师、三个学生,然后模拟老师授课,老师下课的情景
studentlist = [Student("A", 20, 07150200), Student("B", 21, 07150232), Student("C", 19, 07150266)]
teacher = Teacher("PZJ", 20, studentlist)
teacher.teach()
print '-' * 10, "等待下课",'-'*10
time.sleep(5)
teacher.classover()