---
title: po-9. クラス階層，継承
tags:  #python #プログラミング #メソッド #クラス定義 #クラス階層 #継承 #super #dir #python tutor #金子邦彦研究室  
author: [kunihikokaneko](https://www.docswell.com/user/6674398749)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/4EQX4MKL7P.jpg?width=480
description: トピックス：Python, プログラミング, Python Tutor,  クラス定義, メソッド, クラス階層，継承, super, dir  Python プログラミングの基本 https://www.kkaneko.jp/pro/po/index.html  金子邦彦研究室ホームページ https://www.kkaneko.jp/index.html
published: December 21, 21
canonical: https://www.docswell.com/s/6674398749/59W6W5-2021-12-21-210449
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/4EQX4MKL7P.jpg)

po-9. クラス階層，継
承
トピックス：クラス階層，継承（Python Tutor に
よる演習）
URL: https://www.kkaneko.jp/pro/po/index.html
（Python プログラミングの基本）
金子邦彦
1


# Page. 2

![Page Image](https://bcdn.docswell.com/page/KJ46QVP571.jpg)

全体まとめ
• クラス階層では，複数のクラスが親子関係をなす
• 子クラスの定義では，親クラスの指定，親クラスの
__init__ へのアクセスを行う
親クラスの指定
親クラスの __init__ へのアクセス
• 親クラスの属性とメソッドは，子クラスに継承される
• 子クラスにおいて，同じ名前のメソッドが別定義され
ることもある
2


# Page. 3

![Page Image](https://bcdn.docswell.com/page/LE1VP3927G.jpg)

アウトライン
9-1
9-2
9-3
項目
復習
クラスとオブジェクト
クラス定義，オブジェクト生成
演習
3


# Page. 4

![Page Image](https://bcdn.docswell.com/page/GEWL2PQ272.jpg)

クラス Ball
オブジェクト
半径 1，場所（8, 10）
色 blue
オブジェクト
半径 3，場所（2, 4）
色 green
• ２つのオブジェクトともに，
同じクラス Ball
と考えることができる
• オブジェクトは属性を持つ．
半径，場所，色などの属性を
考えることができる．
• メソッドは，オブジェクトに
属する操作や処理．
確認や属性の変化のための
メソッドを考えることができる．
4


# Page. 5

![Page Image](https://bcdn.docswell.com/page/47ZMDKW4E3.jpg)

クラス定義の例
クラス名: Ball
メソッド: __init__, printout
属性: x, y, r, color
※ __init__ は，オブジェクト生成のためのメソッド
5


# Page. 6

![Page Image](https://bcdn.docswell.com/page/YJ6N1QGGJV.jpg)

Python Tutor の起動
① ウェブブラウザを起動する
② Python Tutor を使いたいので，次の URL を開く
http://www.pythontutor.com/
③ 「Python」をクリック
⇒ 編集画面が開く
6


# Page. 7

![Page Image](https://bcdn.docswell.com/page/GJ554RVXJ4.jpg)

Python Tutor でのプログラム実行手順
(1)「Visualize Execution」をク
リックして実行画面に切り替える
(2)「Last」をクリック．
(4)「Edit this code」をク
(3) 実行結果を確認する．
7
リックして編集画面に戻る


# Page. 8

![Page Image](https://bcdn.docswell.com/page/9E2NVD3Q7R.jpg)

Python Tutor 使用上の注意点①
• 実行画面で，次のような赤の表示が出ることがある →
無視してよい
過去の文法ミスに関する確認表示
邪魔なときは「Close」
8


# Page. 9

![Page Image](https://bcdn.docswell.com/page/D7YP321Y7M.jpg)

Python Tutor 使用上の注意点②
「please wait ... executing」のとき，１０秒ほど待つ．
→ 混雑しているときは， 「Server Busy・・・」
というメッセージが出ることがある．
混雑している．少し（数秒から数十秒）待つと自
動で表示が変わる（変わらない場合には，操作を
もう一度行ってみる）
9


# Page. 10

![Page Image](https://bcdn.docswell.com/page/VEN5V82RJ8.jpg)

9-1. クラス階層
10


# Page. 11

![Page Image](https://bcdn.docswell.com/page/Y79Y6K1ZE3.jpg)

クラス階層
クラス階層では，複数のクラスが親子関係をなす
クラスA
親
子
クラスB
親
子
クラスC
親
子
クラスD
11


# Page. 12

![Page Image](https://bcdn.docswell.com/page/G786ZR3YJD.jpg)

クラス Point
クラス Ball
オブジェクト
オブジェクト
場所（1, 2）
色 red
半径 1，場所（8, 10）
色 blue
オブジェクト
半径 3，場所（2, 4）
色 green
12


# Page. 13

![Page Image](https://bcdn.docswell.com/page/L7L9DKV9ER.jpg)

Point クラス定義の例
クラス名: Point
メソッド: __init__, printout
属性: x, y, color
※ __init__ は，オブジェクト生成のためのメソッド
13


# Page. 14

![Page Image](https://bcdn.docswell.com/page/4EMZP2GV7W.jpg)

演習
資料：15 ～ 16
【トピックス】
• クラス定義
• オブジェクト生成
• メソッドアクセス
14


# Page. 15

![Page Image](https://bcdn.docswell.com/page/PER61MZWJ9.jpg)

① Python Tutor のエディタで次のプログラムを入
れる
クラス定義，オブジェクト生成，メソッドアクセス
クラス定義
オブジェクト生成
メソッドアクセス
字下げも正確に
15


# Page. 16

![Page Image](https://bcdn.docswell.com/page/P7XMYV9VJX.jpg)

② 実行し，結果を確認する
メソッド printout
による表示
オブジェクト c
「Visual Execution」をクリック．そして「Last」をクリック．結果を確認．
「Edit this code」をクリックすると，エディタの画面に戻る
16


# Page. 17

![Page Image](https://bcdn.docswell.com/page/37KG3QP5ED.jpg)

Ball クラス定義の例
（クラス階層を考えない場合）
クラス名: Ball
メソッド: __init__, printout
属性: x, y, r, color
※ __init__ は，オブジェクト生成のためのメソッド
17


# Page. 18

![Page Image](https://bcdn.docswell.com/page/LJ3NGPX1J5.jpg)

類似した 2つのクラス
Point
属性
Ball
属性
x
x
y
y
color
メソッド
printout
x, y, color は同じ
r の有り無しが
違う
color
r
メソッド
printout
printout は名前は
同じだが，中身が違う
18


# Page. 19

![Page Image](https://bcdn.docswell.com/page/8JDW1L2KEG.jpg)

Point クラスと Ball クラスの定義の例
（クラス階層を考えない場合）
Point
Ball
x, y, color は同じ
同じようなプログラムを繰り返し書きたいですか？
→ No. クラス階層により解決
19


# Page. 20

![Page Image](https://bcdn.docswell.com/page/VEP9Q21378.jpg)

Point クラスと Ball クラスの定義の例
（クラス階層を考える場合）
Point
Ball
x, y, color について
繰り返し書くことはなくなる
20


# Page. 21

![Page Image](https://bcdn.docswell.com/page/27VM5DYN7Q.jpg)

class Ball(Point)
Ball クラスは Point クラスの子である
Point
Ball
super(Ball, self).__init__(x, y, color)
親クラスである Point クラスの
メソッド __init__ にアクセス．
その引数は x, y, color
21


# Page. 22

![Page Image](https://bcdn.docswell.com/page/5JGG6Y95JL.jpg)

クラス階層を考える場合と考えない場合の違い
Point
Point
働きは
同じ
Ball
クラス階層を考えない
Ball
クラス階層を考える
22


# Page. 23

![Page Image](https://bcdn.docswell.com/page/47QX4MWLJP.jpg)

演習
資料：24 ～ 25
【トピックス】
• サブクラスのクラス定義
23


# Page. 24

![Page Image](https://bcdn.docswell.com/page/KE46QVK5J1.jpg)

① Python Tutor のエディタで次のプログラムを入れる
Ball クラスのクラス定義，オブジェクト生成，
メソッドアクセスを追加
追加
追加
24


# Page. 25

![Page Image](https://bcdn.docswell.com/page/L71VP3X2JG.jpg)

② 実行し，結果を確認する
メソッド printout
による表示
オブジェクト a,
b, c
「Visual Execution」をクリック．そして「Last」をクリック．結果を確認．
「Edit this code」をクリックすると，エディタの画面に戻る
25


# Page. 26

![Page Image](https://bcdn.docswell.com/page/G7WL2PN2J2.jpg)

まとめ
• クラス階層では，複数のクラスが親子関係をなす
• 子クラスの定義では，親クラスの指定，親クラス
の __init__ へのアクセスを行う
親クラスの指定
親クラスの __init__ へのアクセス
26


# Page. 27

![Page Image](https://bcdn.docswell.com/page/4JZMDK3473.jpg)

9-2. 継承
27


# Page. 28

![Page Image](https://bcdn.docswell.com/page/YE6N1QXGEV.jpg)

継承とは，親クラスの属性とメソッドを子クラスが受
け継ぐこと
28


# Page. 29

![Page Image](https://bcdn.docswell.com/page/GE554RXXE4.jpg)

継承
• 親クラスの属性とメソッドは，子クラスに継承
される
• 子クラスにおいて，同じ名前のメソッドが別定
義されることもある
親
Point
子
Ball
・属性 r を追加
・メソッド printout は別定義
29


# Page. 30

![Page Image](https://bcdn.docswell.com/page/972NVDXQJR.jpg)

属性 r を追加
メソッド printout は別定義
30


# Page. 31

![Page Image](https://bcdn.docswell.com/page/DJYP328YJM.jpg)

演習
資料：32 ～ 36
【トピックス】
• 継承
31


# Page. 32

![Page Image](https://bcdn.docswell.com/page/V7N5V8QRE8.jpg)

① Python Tutor のエディタで次のプログラムを入れる
Point クラスに，属性 x と y を 0 にするメソッド reset を追加
追加
追加
32


# Page. 33

![Page Image](https://bcdn.docswell.com/page/YJ9Y6KDZ73.jpg)

② 実行し，結果を確認する
Point クラスのメソッド reset が Ball クラスに継承されていることを確認
メソッド printout
による表示
オブジェクト a, b, c
オブジェクト a, b の属
性 x, y の値が 0 に
なっている
「Visual Execution」をクリック．そして「Last」をクリック．結果を確認．
「Edit this code」をクリックすると，エディタの画面に戻る
33


# Page. 34

![Page Image](https://bcdn.docswell.com/page/GJ86ZR5YED.jpg)

Python では，次のプログラムにより，オブジェク
ト a のメソッド名，属性名などを表示できる
print(dir(a))
34


# Page. 35

![Page Image](https://bcdn.docswell.com/page/LJL9DKY97R.jpg)

③ Python Tutor のエディタで，
プログラムの末尾に「print(dir(a))」を追加する
追加
35


# Page. 36

![Page Image](https://bcdn.docswell.com/page/47MZP2NVJW.jpg)

④ 実行し，結果を確認する
Ball クラスには，Point クラスの属性，メソッドが
継承されている
__class__ など「__」で始まるもの： システムが自動で追加したメソッド，属性
color, printout, r, reset, x, y: プログラムで書いたメソッド，属性
横スクロースする
枠を広げる
「Visual Execution」をクリック．そして「Last」をクリック．結果を確認．
「Edit this code」をクリックすると，エディタの画面に戻る
36


# Page. 37

![Page Image](https://bcdn.docswell.com/page/P7R61MDWE9.jpg)

全体まとめ
• クラス階層では，複数のクラスが親子関係をなす
• 子クラスの定義では，親クラスの指定，親クラスの
__init__ へのアクセスを行う
親クラスの指定
親クラスの __init__ へのアクセス
• 親クラスの属性とメソッドは，子クラスに継承される
• 子クラスにおいて，同じ名前のメソッドが別定義され
ることもある
37


# Page. 38

![Page Image](https://bcdn.docswell.com/page/PJXMYV1VEX.jpg)

関連ページ
• Python まとめページ
https://www.kkaneko.jp/tools/man/python.html
• Python プログラミングを学ぶ
Python Tutor, VisuAlgo, Code Combat を使用
https://www.kkaneko.jp/cc/po/index.html
• Python プログラム例
https://www.kkaneko.jp/pro/python/index.html
• 人工知能の実行（Google Colaboratory を使用）
https://www.kkaneko.jp/pro/ni/index.html
• 人工知能の実行（Python を使用）（Windows 上）
https://www.kkaneko.jp/pro/deepim/index.html
38


