Solution: Inheritance of "Student" and integration into the program
Completion requirements
User.py
Student inherits from User.
class User: # superclass "User"
def __init__(self, surname, firstname, dateOfBirth): # constructor
self._surname = surname # protected properties/attributes => only one "_"
self._firstname = firstname
self._password = firstname[0] + surname[0] + dateOfBirth
self._dateOfBirth = dateOfBirth
self._username = surname
def getSurname(self):
return self._surname
def getFirstname(self):
return self._firstname
def getPassword(self):
return self._password
def getDateOfBirth(self):
return self._password
def getUsername(self):
return self._password
class Student(User): # subclass "Student" inherits every property and method from superclass "User"
def __init__(self, surname, firstname, dateOfBirth, schoolClass):
User.__init__(self, surname, firstname, dateOfBirth) # constructor of superclass is invoked
schoolClass.addStudent(self); # adds himself to school class
self.__schoolClass = schoolClass
def getSchoolClass(self):
return self.__schoolClassBe aware of the bold line in class Student's constructor: when a Student object is created he adds himself to SchoolClass object.
School.py
Only some refactorings in class SchoolClass regarding the new class Student have to be made:
class School:
def __init__(self,schoolClasses):
self.__schoolClasses = schoolClasses
def getSchoolClasses(self):
return self.__schoolClasses
def addSchoolClass(self, schoolClass): # adds a school class to school
self.__schoolClasses.append(schoolClass)
class SchoolClass:
def __init__(self,name, students):
self.__name = name
self.__students = students # list with students
def getName(self):
return self.__name
def getStudents(self):
return self.__students
def addStudent(self, student): # adds a student to school class
self.__students.append(student)testStudent.py
In the main program 3 objects of class Student are created. As fourth parameter the school class is added. As shown above Student's constructor adds student to school class. In this file students no longer have to be assigned to a school class.
from School import * # Import classes
from User import *
schoolClass1 = SchoolClass("c1",[])
schoolClass2 = SchoolClass("c2",[])
student1 = Student("Miller","Sam","22.11.2004",schoolClass1) # additional argument of type SchoolClass
student2 = Student("Smith","Jake","13.02.2003",schoolClass1)
student3 = Student("Hampton","Oliver","11.01.2004",schoolClass2)
school = School([schoolClass1, schoolClass2])
schoolClasses = school.getSchoolClasses()
for sc in schoolClasses:
print(f"School class: {sc.getName()}")
Students = sc.getStudents()
for student in Students: # student now knows school class
print(f"\t{student.getSurname()}, {student.getFirstname()} in school class {student.getSchoolClass().getName()}")Output
School class: c1
Miller, Sam in school class c1
Smith, Jake in school class c1
School class: c2
Hampton, Oliver in school class c2
Last modified: Thursday, 3 October 2019, 8:58 AM