Learn Before
Code

Example of Class Composition

The constructor for Car takes in variables move and stop. The move and stop variables are assigned Move and Stop objects. Thus, Car is composed of Move and Stop classes, making this an example of composition.

class Move(): def __init__(self): print("Move!") class Stop(): def __init__(self): print("Stop!") class Car(): def __init__(self, move, stop): self.move = move self.stop = stop Car(Move(), Stop())

0

1

Updated 2021-06-01

Tags

Python Programming Language

Data Science