230 likes | 347 Views
Föreläsning 7. Klasser och annat: ett exempel. Klassen Course. class Course(object): def __init__(self, code, name, points): self.code = code self.name = name self.points = points def shortString(self): return "%s %s %s hp" % (self.code, self.name, self.points).
E N D
Föreläsning 7 Klasser och annat: ett exempel
Klassen Course class Course(object): def __init__(self, code, name, points): self.code = code self.name = name self.points = points def shortString(self): return "%s %s %s hp" % \ (self.code, self.name, self.points)
__str__-metoden class Course(object): …… def __str__(self): return self.shortString() # Create a course prgi = Course("DD1312", "Programmeringsteknik och Matlab", 8)
Klassen Program class Program(object): def __init__(self, name, code, points): self.name = name self.code = code self.points = points self.compulsoryCourses = [] def shortString(self): return "%s (%s) %d hp" % \ (self.name, self.code, self.points)
En hjälpfunktion def makeInfoString(myList): infoString = "" for element in myList: infoString += \ element.shortString() + "\n" return infoString
Klassen Program def __str__(self): infoString = self.shortString() infoString += \ "\nCompulsory courses:\n" + \ makeInfoString(self.compulsoryCourses) return infoString def addCompulsoryCourse(self, course): self.compulsoryCourses.append(course)
Klassen CourseClass class CourseClass(object): def __init__(self, course, year, period, teacher): self.course = course self.year = year self.period = period self.teacher = teacher self.students = [] def shortString(self): return self.course.shortString() + \ " %s %s given by:\n%s" % \ (self.period, self.year, self.teacher)
Klassen CourseClass def __str__(self): infoString = self.shortString() infoString += \ "\nThe class has the “ + \ ”following students:\n“ infoString += \ makeInfoString(self.students) return infoString def addStudent(self, student): self.students.append(student)
Klassen Person class Person(object): def __init__(self, name, surname, persnr, address, postalcode, city, country = "Sweden", email = None): self.name = name self.surname = surname self.persnr = persnr errorString = "Not a valid personnummer! " + \ "(checksum should be %d)" % self.persnrChecksum() if not self.verifyPersnr(): raise ValueError(errorString) self.address = address self.postalcode = postalcode self.city = city self.country = country self.email = email
Klassen Person def __str__(self): infoString = "Name:\t%s %s\nPersnr:\t%s\n" % \ (self.name, self.surname, self.persnr) infoString += "Address:\n\t%s\n\t%s %s\n\t%s" % \ (self.address, self.postalcode, self.city, self.country) if self.email != None: infoString += "\nEmail: %s\n" % self.email return infoString def getBirthYear(self): birthDigits = self.persnr[0:2] birthYear = 1900 + int(birthDigits) return birthYear
Klassen Person def getAge(self): dateToday = datetime.date.today() thisYear = dateToday.year return thisYear - self.getBirthYear() def getGender(self): genderNumber = int(self.persnr[9]) % 2 if genderNumber == 1: return "male“ else: return "female"
Klassen Person def sumDigitsInStr(str): sum = 0 for d in str: sum += int(d) return sum sumDigitsInStr = staticmethod(sumDigitsInStr) def persnrChecksum(self): noDashContrPersnr = self.persnr[0:6] + self.persnr[7:10] expandedString = "" for i in range(9): if i % 2 == 0: expandedString += str(2 * int(noDashContrPersnr[i])) else: expandedString += noDashContrPersnr[i] return Person.sumDigitsInStr(expandedString) % 10
Klassen Person def verifyPersnr(self): if len(self.persnr) != 11 \ or self.persnr[6] != "-": return false try: int(self.persnr[0:6]) int(self.persnr[7:11]) except: return false return self.persnrChecksum()\ == int(self.persnr[10])
Klassen Teacher class Teacher(Person): def __init__(self, name, surname, persnr, room, address, postalcode, city, country = "Sweden"): super(Teacher, self).__init__(name, surname, persnr, address, postalcode, city, country) self.room = room def __str__(self): infoString = super(Teacher, self).__str__() infoString += "\nRoom:\t%s" % self.room return infoString
Klassen Student class Student(Person): def __init__(self, name, surname, persnr, address, postalcode, city, country = "Sweden", email = None): super(Student, self).__init__(name, surname, persnr, address, postalcode, city, country, email) self.currentCourseClasses = [] self.finishedCourseClasses = []
Klassen Student def __str__(self): infoString = \ super(Student, self).__str__() infoString += "\n\nFinished courses:\n“ infoString += \ makeInfoString(self.finishedCourseClasses) infoString += "\nCurrent courses:\n“ infoString +=\ makeInfoString(self.currentCourseClasses) return infoString
Klassen Student def addCourseClass(self, courseClass): self.currentCourseClasses.append(courseClass) courseClass.addStudent(self) def finishCourseClass(self, code): for courseClass in self.currentCourseClasses: if courseClass.course.code == code: self.finishedCourseClasses.\ append(courseClass) self.currentCourseClasses.\ remove(courseClass) return
Klassen Student def getPoints(self): points = 0 for courseClass in self.finishedCourseClasses: points += courseClass.course.points return points
Att använda klasserna from student import Program, Course, CourseClass, Person, Student, Teacher # Create program indEk = Program("Industriell ekonomi", "I", 180) # Create some courses prgi = Course("DD1312", "Programmeringsteknik och Matlab", 8) envarre = Course("MM0001", "Analys i en variabel", 10) makro = Course("EE0034", "Makroekonomi", 10) print prgi print "“ indEk.addCompulsoryCourse(prgi) indEk.addCompulsoryCourse(envarre) indEk.addCompulsoryCourse(makro) print indEk
Att använda klasserna # Create a person sven = Person("Sven", "Svensson", "750312-0324","Teknikhuset 4", "00000", "Stockholm") print sven print "Age: %d" % sven.getAge() print "Gender: %s" % sven.getGender() print "Birth year: %d" %\ sven.getBirthYear() print ""
Att använda klasserna # Create some teachers douglas = Teacher("Douglas", "Wikström", "750123-0717", "1443","Teknikgatan 23", "12345", "Stockholm") micke = Teacher("Mikael", "Goldmann", "731203-1324", "1444", "Teknikvägen 5", "54321", "Stockholm") stina = Teacher("Stina", "Bengtsson", "711103-1220", "1447", "Teknikvägen 7", "21123", "Stockholm") print douglas print "" print micke print "" print stina print ""
Att använda klasserna # Create some course classes prgi07 = CourseClass(prgi, 2007, "ht", douglas) makro06 = CourseClass(makro, 2006, "vt", micke) makro05 = CourseClass(makro, 2005, "vt", douglas) envarre07 = CourseClass(envarre, 2007, "ht", stina)
Att använda klasserna # Create some students olle = Student("Olle", "Olsson", "840104-1515", "Ollevägen 7", "67432", "Nacka") lisa = Student("Lisa", "Ek", "850315-1410", "Klasvägen 5", "62789", "Örby") olle.addCourseClass(prgi07) olle.addCourseClass(makro05) lisa.addCourseClass(prgi07) lisa.addCourseClass(makro06) lisa.addCourseClass(envarre07) print prgi07 print "" lisa.finishCourseClass("DD1312") lisa.finishCourseClass("MM0001") print lisa print ""