首页 > 代码库 > python:使用静态方法,__str__函数
python:使用静态方法,__str__函数
class Man(object): def __init__(self,name,phone): self.name=name self.phone=phone def Info_p(self): return "Person %s has phone %s "%(self.name,self.phone) class Student(Man): gpa_total = [] def __init__(self,name,phone,gpa): Man.__init__(self,name,phone) self.gpa=gpa Student.gpa_total.append(self.gpa) @staticmethod def mean_gpa(): return sum(Student.gpa_total) / float(len(Student.gpa_total)) def info_gpa(self): reply = self.Info_p() reply += "\n and is Student with gpa %s "%self.gpa return reply def __str__(self): return self.info_gpa() class Employee(Man): emp_total = [] def __init__(self,name,phone,empolyee): Man.__init__(self,name,phone) self.empolyee=empolyee Employee.emp_total.append(self.empolyee) @staticmethod def total_salary(): return sum(Employee.emp_total) def info_em(self): reply = self.Info_p() reply += "\n and is an Employee with salary %s"%(self.empolyee) return reply class Staff(Employee): def __init__(self,name,phone,empolyee,staff): Employee.__init__(self,name,phone,empolyee) self.staff=staff def info_staff(self): reply = self.info_em() reply += "\n and is Staff with title %s"%self.staff return reply def __str__(self): return self.info_staff() class Professor(Employee): def __init__(self,name,phone,empolyee,mingchen): Employee.__init__(self,name,phone,empolyee) self.mingchen=mingchen def Info_M(self): reply = self.info_em() reply += "\n and is a Professor assigned to class %s"%self.mingchen return reply def __str__(self): return self.Info_M() People = [Student("Sandy","326-8324",3.65),Student("Jordan","632-7434",3.1),Professor("Leslie","985-2363",50000.00,"Info 501"),Staff("Alex","743-4638",25000.00,"Editor")] print "These are the people in the university: " for person in People: print person print print "Our total university payroll budget is :" +str(Employee.total_salary()) print "Our average student GPA is :" +str(Student.mean_gpa())
These are the people in the university: Person Sandy has phone 326-8324 and is Student with gpa 3.65 Person Jordan has phone 632-7434 and is Student with gpa 3.1 Person Leslie has phone 985-2363 and is an Employee with salary 50000.0 and is a Professor assigned to class Info 501 Person Alex has phone 743-4638 and is an Employee with salary 25000.0 and is Staff with title Editor Our total university payroll budget is :75000.0 Our average student GPA is :3.375
本文出自 “知识在于积累” 博客,请务必保留此出处http://goooood.blog.51cto.com/5060201/1579488
python:使用静态方法,__str__函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。