python
class Car(object):
def run(self):
print('run')
class ToyotaCar(Car):
pass
class TeslaCar(Car):
def autorun(self):
print('autorun')
car = Car()
car.run()
print('#############')
toyota_car = ToyotaCar()
toyota_car.run()
print('#############')
tesla_car = TeslaCar()
tesla_car.run()
tesla_car.autorun()
Execution result
run
#############
run
#############
run
autorun
First, I defined a class called Car.
Then, I created a ToyotoCar class that inherits that Car. The method of ToyotoCar class is Only the run method exactly the same as the Car class.
The TeslaCar class is also a class that inherits the Car class, There is an autorun method as well as a run method.
Recommended Posts