Oct-29-2017, 10:43 PM
I have a project I was working on in Java, and for fun, I decided to implement it in Python to see the amount of code and result. My requirements are as follows:
We have two types of Students, Domestic and International. Domestic students, don't require documentation, while international students do. I immediately thought of inheritance, and how it might be implemented. This is what I came up with:
Abstract Student base class:
We have two types of Students, Domestic and International. Domestic students, don't require documentation, while international students do. I immediately thought of inheritance, and how it might be implemented. This is what I came up with:
Abstract Student base class:
from abc import ABCMeta class Student(metaclass=ABCMeta): def __init__(self, id, firstname, lastname): self.__id = id self.__firstname = firstname self.__lastname = lastname @property def iden(self): return self.__id @property def first_name(self): return self.__firstname @property def last_name(self): return self.__lastnameDomestic:
from Student import Student class Domestic(Student): def __init__(self, iden, firstname, lastname): super().__init__(iden, firstname, lastname) self.__type_of_student = "Domestic" @property def student_type(self): return self.__type_of_student def __str__(self): return "ID: " + self.iden + " " + " First: " + self.first_name + " Last Name: " + self.last_name + " " + " Type: " + self.student_typeInernational:
from Student import Student from copy import deepcopy class International(Student): def __init__(self, iden, firstname, lastname, docuemnts): super().__init__(iden, firstname, lastname) self.__documents = deepcopy(docuemnts) self.__type_of_student = "International" @property def international_documents(self): return deepcopy(self.__documents) def __str__(self): return "ID: " + self.iden + " First Name: " + self.first_name + " Lsat Name: " + \ self.last_name + " Type: " + self.__type_of_studentI was thinking maybe there was another way to solve this without the use of inheritance, I'm just not seeing it. I don't mind if I have to use inheritance, but this is just a class that's a data container with rules. Is there another structure that I can use to implement/enforce those rules?