Python学习第八天——学校(类的继承)
2018-06-18 02:36:55来源:未知 阅读 ()
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)
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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash