python模拟老师授课下课情景

 1 # -*- coding:utf-8 -*-
 2 import time
 3 
 4 class Person(object):
 5     '''
 6     定义父类:人
 7     属性:姓名,年龄
 8     方法:走路(打印:姓名+“正在走路”)
 9     '''
10      
11     def __init__(self, name, age):
12         self.name = name
13         self.age = age
14 
15     def walk(self):
16         print self.name + "正在走路"
17 
18 
19 class Teacher(Person):
20     '''
21     定义子类:老师
22     属性:上课学生(集合)
23     方法:授课(打印:姓名+“老师正在上课”, 并调用所有上课学生的听课方法)
24           下课(打印:“下课”,并调用所有学生的走路方法)
25     '''
26     # 方法重载(name), 覆盖父类初始值
27     def __init__(self,name,age, students):
28         Person.__init__(self, name, age)
29         self.students = students
30 
31     def teach(self):
32         print self.name + "老师正在上课"
33         for i in self.students:
34             i.learn()
35 
36     def classover(self):
37         print "下课"
38         for i in self.students:
39             i.walk()
40 
41 
42 class Student(Person):
43     '''
44     定义子类:学生
45     属性:学号
46     方法:听课(打印:姓名+学号+“正在听课”)
47     '''
48     def __init__(self, name,age, studentID):
49         Person.__init__(self, name, age)
50         self.studentID = studentID
51 
52     def learn(self):
53         print self.name + str(self.studentID) + "正在听课"
54 
55 
56 if __name__ == "__main__":
57 
58     # 实例化一个老师、三个学生,然后模拟老师授课,老师下课的情景
59     studentlist = [Student("A", 20, 00000200), Student("B", 21, 00000232), Student("C", 19, 00000266)]
60     teacher = Teacher("xxx", 20, studentlist)
61     teacher.teach()
62     print '-' * 10, "等待下课",'-'*10
63     time.sleep(5)
64     teacher.classover()

 

 
posted @ 2017-12-27 12:26  Panisme  阅读(396)  评论(0编辑  收藏  举报