1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

クラスの継承

Posted at
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()
実行結果
run
#############
run
#############
run
autorun

まず、Carというクラスを定義した。

そしてそのCarを継承したToyotoCarクラスを作成した。
ToyotoCarクラスのもつメソッドは、
Carクラスと全く同じrunメソッドのみ。

TeslaCarクラスもCarクラスを継承したクラスだが、
runメソッドだけでなくautorunメソッドもある。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?