Section 19.3 Extending
If you compare the code in the __init__ methods of
LabeledPoint, you can see that there is some duplication–the initialization of x and y. We can eliminate the duplication by having
__init__()method. That way, each class will be responsible for initializing its own instance variables.
A method in a child class that overrides a method in the parent can invoke the overridden method using
super(), like this:
class LabeledPoint(Point): def __init__(self, initX, initY, label): super().__init__(initX, initY) self.label = label
In this example, line 4 invokes the
Point, passing the values of
initYto be used in initializing the
Here is a complete code listing showing both classes, with a version of
LabeledPointthat invokes its parent's implementation using
super()to avoid duplicating the functionality provided in
You have attempted of activities on this page.