pf-8. メソッド,クラス,コンストラクタ,継承

236 Views

January 29, 23

スライド概要

トピックス:Python, Google Colaboratory, クラス, メソッド, コンストラクタ, 継承, スーパークラス, サブクラス, class, def, __init__, self, vars, super

Python 入門(Google Colaboratory を使用)(全8回)
https://www.kkaneko.jp/pro/pf/index.html

金子邦彦研究室ホームページ
https://www.kkaneko.jp/index.html

profile-image

金子邦彦(かねこくにひこ) 福山大学・工学部・教授 ホームページ: https://www.kkaneko.jp/index.html 金子邦彦 YouTube チャンネル: https://youtube.com/user/kunihikokaneko

シェア

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

pf-8. メソッド,クラス,コン ストラクタ,継承 (Python 入門,Google Colaboratory を使用) (全8回) URL: https://www.kkaneko.jp/pro/pf/index.html 金子邦彦 1

2.

オブジェクトとメソッド hero.moveDown() hero オブジェクト moveDown() メソッド 間を「.」で区切っている • オブジェクト コンピュータでの操作や処理の対象となるもの ※ 名前の付いたオブジェクトのことを変数,関数な どと呼んだりもする • メソッド オブジェクトに属する操作や処理 2

3.

クラス クラスは,同じ種類のオブジェクトの集まりと考え ることができる 人間 学生 学生でもあり人間でもある 人間だが、学生ではない 3

4.

Python のクラスとオブジェクト • 次の2つのオブジェクトを生成する Python プロ グラム x 5 170.51 'apple' y 3 40.97 ‘orange' qty weight name x = C(5, 170.51, 'apple') y = C(3, 40.97, 'orange') Python プログラム • このとき,次のクラスを使うことにする クラス名 C 属性 qty, weight, name class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name Python プログラム 4

5.

コンストラクタ コンストラクタは,オブジェクトの生成を行うメ ソッドである. class C(object): コンストラクタ def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name 5

6.

属性アクセスとメソッドアクセス • 「.」+メソッド名によるメソッドアクセス • 「.」+属性名による属性アクセス 6

7.

メソッド定義内 • メソッド定義内では,self +「.」で属性やメソッ ドにアクセス class C(): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name def total(self): return self.qty * self.weight 7

8.

属性の一括取得 vars は,オブジェクトの属性名と値を一度に得る 8

9.

スーパークラス,サブクラス • スーパークラス「人間」 • サブクラス「学生」 「学生」のオブジェクトは,すべて「人間」である 人間 学生 学生でもあり人間でもある 人間だが、学生ではない 9

10.

継承 • 継承は,スーパークラスの属性とメソッドをサブ クラスが受け継ぐこと • 但し,コンストラクタは受け継がない 10

11.

継承 クラス名 C 属性 qty, weight, name class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name def total(self): return self.qty * self.weight class E(C): def __init__(self, qty, weight, name, price): super(E, self).__init__(qty, weight, name) self.price = price def payment(self): return self.qty * self.price クラス名 E 属性 qty, weight, name, price クラス E は,スーパークラスで あるクラス C の属性を継承する. 11

12.

まとめ • class クラス定義 • __init__ コンストラクタ • self セス クラス定義内での自オブジェクトへアク • vars オブジェクトの属性名と値 • super 親クラス(スーパークラス) 12

13.

演習 資料:14 ~ 17 【トピックス】 • クラス定義 • コンストラクタ 13

14.

① Google Colaboratory のWebページを開く https://colab.research.google.com 14

15.

② 「ファイル」で,「ノートブックを新規作成」を選ぶ ③ Google アカウントでのログインが求められたときはログ インする 15

16.

④ コードセルを新規作成し,Python プログラムを入れる.結果を確認. class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name x = C(5, 170.51, 'apple') print(vars(x)) y = C(3, 40.97, 'orange') print(vars(y)) 16

17.

⑤ コードセルを新規作成し,Python プログラムを入れる.結果を確認. class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name def total(self): return self.qty * self.weight class E(C): def __init__(self, qty, weight, name, price): super().__init__(qty, weight, name) self.price = price def payment(self): return self.qty * self.price x2 = E(2, 875.34, 'melon', 500) vars(x2) print(x2.total()) print(x2.payment()) type(x2) 17

18.

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