0

[Here is the UML diagram][2][2]: https://i.stack.imgur.com/jSnIn.png

I want to write a python program to implement this diagram. I've created a package called CarPackage and inside of it i've put those 3 classes( Car, Motor, Camera). I don't know exactly how to link classes togheter ,the python sintax gives me trouble.. I've searched over the internet but couldn't find anything useful.

This is my code until now:

class Car:
    Motor = []
    def __init__(self, Motor,  Camera):
        self.__Motor = Motor # private attribute
        self.__Camera = Camera()  # private attribute

        def move_forward(self):
            print("Moving forward")

        def move_backward(self):
            print("Moving backward")

        def stop(self):
            print("The car has stopped")

        def take_picture(self):
            print("Taking picture...")```




from CarPackage import Car

class Camera(Car):
    resolution_X = int
    resolution_Y = int
    rotation = int

    def __init__(self, resolution_X, resolution_Y, rotation):
        self.__X = resolution_X
        self.__Y = resolution_Y
        self.__rot = rotation

    def take_picture(fileName):
        print("Taking picture...")




from CarPackage import Car

class Motor(Car):
    forward_pin = int
    backward_pin = int

    def __init__(self, forward_pin, backward_pin):
        self.__forward = forward_pin  # private attribute
        self.__backward = backward_pin  # private attribute

    def move_forward(self):
        print("Moving forward")

    def move_backward(self):
        print("Moving backward")

    def stop(self):
        print("The car has stopped")



HeaveHd
  • 13
  • 2

1 Answers1

0

If I understand correctly, remove 'car' from Motor(Car) and Camera(Car), then change car class to

class Car(Motor, Camera)
...

Then Car will inherit the methods and attributes of Motor and Camera, but not the init method. If it is important to isolate the references, then you might make the parent object an attribute of the child's instance and vice versa, and then adjust methods to call upon those attributes or refer to those attributes as necessary.

Another thought is to use directly call the init method of Camera and Motor to capture their initialized attributes.

class Car(Motor, Camera):
    Motor.__init__(self)
    Camera.__init__(self)

Also see Calling parent class __init__ with multiple inheritance, what's the right way? for good description of using super() (that is, someone with more python and computer experience than I had an answer!).