标签:python return staticmethod __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 return staticmethod __str__
原文地址:http://goooood.blog.51cto.com/5060201/1579488