1 / 23

Föreläsning 7

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).

kera
Download Presentation

Föreläsning 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Föreläsning 7 Klasser och annat: ett exempel

  2. 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)

  3. __str__-metoden class Course(object): …… def __str__(self): return self.shortString() # Create a course prgi = Course("DD1312", "Programmeringsteknik och Matlab", 8)

  4. 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)

  5. En hjälpfunktion def makeInfoString(myList): infoString = "" for element in myList: infoString += \ element.shortString() + "\n" return infoString

  6. 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)

  7. 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)

  8. 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)

  9. 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

  10. 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

  11. 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"

  12. 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

  13. 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])

  14. 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

  15. 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 = []

  16. 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

  17. 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

  18. Klassen Student def getPoints(self): points = 0 for courseClass in self.finishedCourseClasses: points += courseClass.course.points return points

  19. 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

  20. 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 ""

  21. 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 ""

  22. 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)

  23. 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 ""

More Related