Successfully reported this slideshow.
Your SlideShare is downloading. ×

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

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Python の基本
Python の基本
Loading in …3
×

Check these out next

1 of 18 Ad

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

Download to read offline

トピックス: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

トピックス: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

Advertisement
Advertisement

More Related Content

More from kunihikokaneko1 (20)

Advertisement

Recently uploaded (20)

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

  1. 1. pf-8. メソッド,クラス,コン ストラクタ,継承 (Python 入門,Google Colaboratory を使用) (全8回) URL: https://www.kkaneko.jp/pro/pf/index.html 1 金子邦彦
  2. 2. オブジェクトとメソッド • オブジェクト コンピュータでの操作や処理の対象となるもの ※ 名前の付いたオブジェクトのことを変数,関数な どと呼んだりもする • メソッド オブジェクトに属する操作や処理 2 hero.moveDown() hero オブジェクト moveDown() メソッド 間を「.」で区切っている
  3. 3. クラス クラスは,同じ種類のオブジェクトの集まりと考え ることができる 3 人間 学生 学生でもあり人間でもある 人間だが、学生ではない
  4. 4. Python のクラスとオブジェクト • 次の2つのオブジェクトを生成する Python プロ グラム • このとき,次のクラスを使うことにする 4 クラス名 C 属性 qty, weight, name 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 プログラム class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name Python プログラム
  5. 5. コンストラクタ コンストラクタは,オブジェクトの生成を行うメ ソッドである. 5 class C(object): def __init__(self, qty, weight, name): self.qty = qty self.weight = weight self.name = name コンストラクタ
  6. 6. 属性アクセスとメソッドアクセス • 「.」+メソッド名によるメソッドアクセス • 「.」+属性名による属性アクセス 6
  7. 7. メソッド定義内 • メソッド定義内では,self +「.」で属性やメソッ ドにアクセス 7 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
  8. 8. 属性の一括取得 vars は,オブジェクトの属性名と値を一度に得る 8
  9. 9. スーパークラス,サブクラス • スーパークラス「人間」 • サブクラス「学生」 「学生」のオブジェクトは,すべて「人間」である 9 人間 学生 学生でもあり人間でもある 人間だが、学生ではない
  10. 10. 継承 • 継承は,スーパークラスの属性とメソッドをサブ クラスが受け継ぐこと • 但し,コンストラクタは受け継がない 10
  11. 11. 継承 クラス名 C 属性 qty, weight, name 11 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 の属性を継承する.
  12. 12. まとめ • class クラス定義 • __init__ コンストラクタ • self クラス定義内での自オブジェクトへアク セス • vars オブジェクトの属性名と値 • super 親クラス(スーパークラス) 12
  13. 13. 演習 資料:14 ~ 17 【トピックス】 • クラス定義 • コンストラクタ 13
  14. 14. ① Google Colaboratory のWebページを開く https://colab.research.google.com 14
  15. 15. ② 「ファイル」で,「ノートブックを新規作成」を選ぶ ③ Google アカウントでのログインが求められたときはログ インする 15
  16. 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. 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. 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

×