As to the original question - I think the idea is to have __init__() in the child class that will override the parent/super class __init__(). You will need also to initialize the super class using
super()
. Otherwise it doesn't make sense to have a child class that inherits. And it's clear "the teacher" is using getters and setters and for attribute3 these are in the child class, not in parent class. If you add a3 to parent class, I guess you would be expected to add also the getters and setters and then it does not make sense to have them also in the child class. So what I would do for the original question:
class SuperClass:
def __init__(self, a1, a2):
self.attribute1 = a1
self.attribute2 = a2
def getAttribute1(self):
return self.attribute1
def setAttribute1(self, a1):
self.attribute1 = a1
def getAttribute2(self):
return self.attribute2
def setAttribute1(self, a2):
self.attribute2 = a2
class SubClass(SuperClass):
def __init__(self, a1, a2, a3):
super(SubClass, self).__init__(a1, a2)
self.attribute3 = a3
def getAttribute3(self):
return self.attribute3
def setAttribute3(self, a3):
self.attribute3 = a3
def printMessage(self):
print("My attributes are: ", self.getAttribute1(), \
self.getAttribute2(), self.getAttribute3())
testObject = SubClass("i", "am", "awesome!")
testObject.printMessage()
Output:
My attributes are: i am awesome!
(Feb-02-2019, 06:50 AM)sonedap Wrote: [ -> ]so,in python how should this code be???
class SuperClass:
def __init__(self, a1, a2):
self.attribute1 = a1
self.attribute2 = a2
class SubClass(SuperClass):
def __init__(self, a1, a2, a3):
super(SubClass, self).__init__(a1, a2)
self.attribute3 = a3
def print_message(self):
print("My attributes are: ", self.attribute1, self.attribute2, self.attribute3)
# actually I would use str.format() or f-strings
# print(f"My attributes are: {self.attribute1} {self.attribute2} {self.attribute3}")
test_object = SubClass("i", "am", "awesome!")
test_object.print_message()
Of course there might be cases when you need getters and setters, but in most cases you probably don'y need them
a read that you may find interesting:
Python Is Not Java
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.
In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.