面向对象,类的继承

来源:互联网 发布:ip攻击软件 编辑:程序博客网 时间:2024/04/28 23:18

创建一个基本的Person类,由此作为基类,并派生出新类。

import datetimeclass Person(object):    def __init__(self, name):        """create a person called name"""        self.name = name        self.birthday = None        self.lastName = name.split(' ')[-1]    def getLastName(self):        """return self's last name"""        return self.lastName    def setBirthday(self,month,day,year):        """sets self's birthday to birthDate"""        self.birthday = datetime.date(year,month,day)    def getAge(self):        """returns self's current age in days"""        if self.birthday == None:            raise ValueError        return (datetime.date.today() - self.birthday).days    def __lt__(self, other):        """return True if self's ame is lexicographically           less than other's name, and False otherwise"""        if self.lastName == other.lastName:            return self.name < other.name        return self.lastName < other.lastName    def __str__(self):        """return self's name"""        return self.name# me = Person("William Eric Grimson")# print me# me.getLastName()# me.setBirthday(1,2,1927)# me.getAge()# her = Person("Cher")# her.getLastName()# plist = [me, her]# for p in plist: print p# plist.sort()# for p in plist: print pclass MITPerson(Person):    nextIdNum = 0 # next ID number to assign    def __init__(self, name):        Person.__init__(self, name) # initialize Person attributes        # new MITPerson attribute: a unique ID number        self.idNum = MITPerson.nextIdNum        MITPerson.nextIdNum += 1    def getIdNum(self):        return self.idNum    # sorting MIT people uses their ID number, not name!    def __lt__(self, other):        return self.idNum < other.idNump1 = MITPerson('Eric')p2 = MITPerson('John')p3 = MITPerson('John')p4 = Person('John')# print p1# p1.getIdNum()# p2.getIdNum()# p1 < p2# p3 < p2# p4 < p1# p1 < p4

运行结果:


发生错误的原因,应值得思考。


改进:

import datetimeclass Person(object):    def __init__(self, name):        """create a person called name"""        self.name = name        self.birthday = None        self.lastName = name.split(' ')[-1]    def getLastName(self):        """return self's last name"""        return self.lastName    def setBirthday(self,month,day,year):        """sets self's birthday to birthDate"""        self.birthday = datetime.date(year,month,day)    def getAge(self):        """returns self's current age in days"""        if self.birthday == None:            raise ValueError        return (datetime.date.today() - self.birthday).days    def __lt__(self, other):        """return True if self's ame is lexicographically           less than other's name, and False otherwise"""        if self.lastName == other.lastName:            return self.name < other.name        return self.lastName < other.lastName    def __str__(self):        """return self's name"""        return self.name# me = Person("William Eric Grimson")# print me# me.getLastName()# me.setBirthday(1,2,1927)# me.getAge()# her = Person("Cher")# her.getLastName()# plist = [me, her]# for p in plist: print p# plist.sort()# for p in plist: print pclass MITPerson(Person):    nextIdNum = 0 # next ID number to assign    def __init__(self, name):        Person.__init__(self, name) # initialize Person attributes        # new MITPerson attribute: a unique ID number        self.idNum = MITPerson.nextIdNum        MITPerson.nextIdNum += 1    def getIdNum(self):        return self.idNum    # sorting MIT people uses their ID number, not name!    def __lt__(self, other):        return self.idNum < other.idNum# p1 = MITPerson('Eric')# p2 = MITPerson('John')# p3 = MITPerson('John')# p4 = Person('John')# print p1# p1.getIdNum()# p2.getIdNum()# p1 < p2# p3 < p2# p4 < p1# p1 < p4class UG(MITPerson):    def __init__(self, name, classYear):        MITPerson.__init__(self, name)        self.year = classYear    def getClass(self):        return self.yearclass Grad(MITPerson):    passdef isStudent(obj):    return isinstance(obj,UG) or isinstance(obj,Grad)#s1 = UG('Fred', 2016)#s2 = Grad('Angela')#isStudent(s1)#isStudent(s2)class TransferStudent(MITPerson):    pass# go back and define# class Student(MITPerson)# change inheritance for UG, Grad and TransferStudent# change def isStudent(obj):#            return isinstance(obj, Student)

运行结果:


改进:

import datetimeclass Person(object):    def __init__(self, name):        """create a person called name"""        self.name = name        self.birthday = None        self.lastName = name.split(' ')[-1]    def getLastName(self):        """return self's last name"""        return self.lastName    def setBirthday(self,month,day,year):        """sets self's birthday to birthDate"""        self.birthday = datetime.date(year,month,day)    def getAge(self):        """returns self's current age in days"""        if self.birthday == None:            raise ValueError        return (datetime.date.today() - self.birthday).days    def __lt__(self, other):        """return True if self's ame is lexicographically           less than other's name, and False otherwise"""        if self.lastName == other.lastName:            return self.name < other.name        return self.lastName < other.lastName    def __str__(self):        """return self's name"""        return self.name# me = Person("William Eric Grimson")# print me# me.getLastName()# me.setBirthday(1,2,1927)# me.getAge()# her = Person("Cher")# her.getLastName()# plist = [me, her]# for p in plist: print p# plist.sort()# for p in plist: print pclass MITPerson(Person):    nextIdNum = 0 # next ID number to assign    def __init__(self, name):        Person.__init__(self, name) # initialize Person attributes        # new MITPerson attribute: a unique ID number        self.idNum = MITPerson.nextIdNum        MITPerson.nextIdNum += 1    def getIdNum(self):        return self.idNum    # sorting MIT people uses their ID number, not name!    def __lt__(self, other):        return self.idNum < other.idNum# p1 = MITPerson('Eric')# p2 = MITPerson('John')# p3 = MITPerson('John')# p4 = Person('John')# print p1# p1.getIdNum()# p2.getIdNum()# p1 < p2# p3 < p2# p4 < p1# p1 < p4class UG(MITPerson):    def __init__(self, name, classYear):        MITPerson.__init__(self, name)        self.year = classYear    def getClass(self):        return self.yearclass Grad(MITPerson):    passdef isStudent(obj):    return isinstance(obj,UG) or isinstance(obj,Grad)#s1 = UG('Fred', 2016)#s2 = Grad('Angela')#isStudent(s1)#isStudent(s2)class TransferStudent(MITPerson):    pass# go back and define# class Student(MITPerson)# change inheritance for UG, Grad and TransferStudent# change def isStudent(obj):#            return isinstance(obj, Student)class Grades(object):    """A mapping from students to a list of grades"""    def __init__(self):        """Create empty grade book"""        self.students = []  # list of Student objects        self.grades = {}    # maps idNum -> list of grades        self.isSorted = True # true if self.students is sorted    def addStudent(self, student):        """Assumes: student is of type Student           Add student to the grade book"""        if student in self.students:            raise ValueError('Duplicate student')        self.students.append(student)        self.grades[student.getIdNum()] = []        self.isSorted = False    def addGrade(self, student, grade):        """Assumes: grade is a float           Add grade to the list of grades for student"""        try:            self.grades[student.getIdNum()].append(grade)        except KeyError:            raise ValueError('Student not in grade book')    def getGrades(self, student):        """Return a list of grades for student"""        try:    # return copy of student's grades            return self.grades[student.getIdNum()][:]        except KeyError:            raise ValueError('Student not in grade book')    def allStudents(self):        """Return a list of the students in the grade book"""        if not self.isSorted:            self.students.sort()            self.isSorted = True        return self.students[:] #return copy of list of studentsdef gradeReport(course):    """Assumes: course if of type grades"""    report = []    for s in course.allStudents():        tot = 0.0        numGrades = 0        for g in course.getGrades(s):            tot += g            numGrades += 1        try:            average = tot/numGrades            report.append(str(s) + '\'s mean grade is '                          + str(average))        except ZeroDivisionError:            report.append(str(s) + ' has no grades')    return '\n'.join(report)ug1 = UG('Jane Doe', 2014)ug2 = UG('John Doe', 2015)ug3 = UG('David Henry', 2003)g1 = Grad('John Henry')g2 = Grad('George Steinbrenner')six00 = Grades()six00.addStudent(g1)six00.addStudent(ug2)six00.addStudent(ug1)six00.addStudent(g2)for s in six00.allStudents():    six00.addGrade(s, 75)six00.addGrade(g1, 100)six00.addGrade(g2, 25)six00.addStudent(ug3)#print gradeReport(six00)

运行结果:


0 0
原创粉丝点击