[PYTHON] Class inheritance

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

Class inheritance
[Python] Class inheritance, override
class
Class inheritance and super functions
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Class variables
I looked into class inheritance overrides.
Class variables
Multiple inheritance
Python #inheritance (inheritance)
Abstract class
Comparison of class inheritance and constructor description
[Python] class, instance
"Kanrika" python class
Try to solve the Python class inheritance problem
About python, class
About python inheritance
Go class basics
Trouble with Python pseudo-private variables and class inheritance
Page processing class
Class inheritance practice in python as seen in sklearn
Class method static method
Python class, instance
Proprietary exception class
JSON conversion class
#Python basics (class)