1 #-*-coding:utf-8-*-2 Import Time3 4 classPerson (object):5 " "6 define Parent class: People7 Properties: Name, age8 method: Walk (Print: name + "Walking")9 " "Ten One def __init__(self, Name, age): ASelf.name =name -Self.age = Age - the defWalk (self): - PrintSelf.name +"is walking" - - + classTeacher (person): - " " + definition Subclass: Teacher A Properties: Class Students (collection) at method: Teach (Print: Name + "Teacher is in class", and call all class students to listen to the method) - class dismissed (print: "Class dismissed" and call all students on the way to walk) - " " - #method overload (name), overriding the parent class initial value - def __init__(Self,name,age, students): -Person.__init__(self, name, age) inSelf.students =Students - to defTeach (self): + PrintSelf.name +"The teacher is having a class" - forIinchself.students: the I.learn () * $ defClassover (self):Panax Notoginseng Print "class" - forIinchself.students: the I.walk () + A the classStudent (person): + " " - defining subclasses: Students $ Properties: Study number $ method: Lectures (Print: Name + School Number + "listening class") - " " - def __init__(self, Name,age, StudentID): thePerson.__init__(self, name, age) -Self.studentid =StudentIDWuyi the defLearn (self): - PrintSelf.name + str (self.studentid) +"I 'm listening ." Wu - About if __name__=="__main__": $ - #instantiate a teacher, three students, then mock the teacher class, the teacher dismissed the scene -Studentlist = [Student ("A", 07150200), Student ("B", 07150232), Student ("C", 19, 07150266)] -Teacher = Teacher ("Pzj", 20, Studentlist) A Teacher.teach () + Print '-'* 10,"waiting for class .",'-'*10 theTime.sleep (5) -Teacher.classover ()
Python Mock Teacher classes class