Python学习第八天——学校(类的继承)

 1 #coding=utf-8
 2 #Version:python 3.6.0
 3 #Tools:Pycharm 2017.3.2
 4 _date_ = '2018/4/25/025 21:02'
 5 
 6 class school(object):
 7     def __init__(self,name,addr):
 8         self.name = name
 9         self.addr = addr
10         self.students = []
11         self.teachers = []
12         self.staffs = []
13 
14     def enroll(self,stu_obj):
15         print("student %s enrolls in"%stu_obj.name)
16         self.students.append(stu_obj)
17 
18     def hire(self,staff_obj):
19         self.staffs.append(staff_obj)
20         print("hire new staff %s"%staff_obj.name)
21 
22 class schoolmember(object):
23     def __init__(self,name,age,sex):
24         self.name = name
25         self.age = age
26         self.sex = sex
27 
28     def tell(self):
29         pass
30 
31 class teacher(schoolmember):
32     def __init__(self,name,age,sex,salary,course):
33         super(teacher,self).__init__(name,age,sex)
34         self.salary = salary
35         self.course = course
36 
37     def tell(self):
38         print('''
39         -----info of teacher:%s-----
40         name:%s
41         age:%s
42         sex:%s
43         salary:%s
44         course:%s
45         '''%(self.name,self.name,self.age,self.sex,self.salary,self.course))
46 
47     def teach(self):
48         print("%s is teaching %s"%(self.name,self.course))
49 
50 class student(schoolmember):
51     def __init__(self,name,age,sex,stu_id,grade):
52         super(student,self).__init__(name,age,sex)
53         self.stu_id = stu_id
54         self.grade = grade
55 
56     def tell(self):
57         print('''
58         -----info of student %s-----
59         name:%s
60         age:%s
61         sex:%s
62         stu_id:%s
63         grade:%s
64         '''%(self.name,self.name,self.age,self.sex,self.stu_id,self.grade))
65 
66     def pay(self,amount):
67         print("%s has paid tution for $%s"%(self.name,amount))
68 
69 school = school("hnust","xiangtan")
70 t1 = teacher("adam",28,"male",200000,"python")
71 t2 = teacher("eva",28,"female",200000,"music")
72 s1 = student("alex",22,"male",1404020422,"opp-1")
73 s2 = student("eason",19,"male",1404020423,"opp-2")
74 
75 t1.tell()
76 s1.tell()
77 school.hire(t1)
78 school.hire(t2)
79 school.enroll(s1)
80 school.enroll(s2)
81 
82 print(school.students)
83 print(school.staffs)
84 
85 school.staffs[0].teach()
86 for stu in school.students:
87     stu.pay(500)
View Code
E:\ProgramData\Anaconda3\python.exe D:/Python_proc/s14/week6/day1/继承-学校.py

        -----info of teacher:adam-----
        name:adam
        age:28
        sex:male
        salary:200000
        course:python
        

        -----info of student alex-----
        name:alex
        age:22
        sex:male
        stu_id:1404020422
        grade:opp-1
        
hire new staff adam
hire new staff eva
student alex enrolls in
student eason enrolls in
[<__main__.student object at 0x0000028F0A59D8D0>, <__main__.student object at 0x0000028F0A59D908>]
[<__main__.teacher object at 0x0000028F0A59D860>, <__main__.teacher object at 0x0000028F0A59D898>]
adam is teaching python
alex has paid tution for $500
eason has paid tution for $500

Process finished with exit code 0

  

猜你喜欢

转载自www.cnblogs.com/yongor/p/8947498.html