SlideShare a Scribd company logo
1 of 20
K3Python講座2018 第7回
オブジェクト指向入門コース 第3回
前回の復習
(*>△<)ナーンナーン!
チャレンジ問題(再帰)
• 正方形のタイルが敷き詰められた長方形の部屋がある.
• タイルの色は赤か黒 である.
• 最初に一人の人が部屋の黒いタイルの上に立っている.
• あるタイルからは隣接する四つのタイルに移動することができる.
• ただし,赤いタイルの上に移動することはできない.
• 移動できるのは黒いタイルの上だけである.
上記の移動を繰り返すことによって到達できるタイルの数を答えるプロ
グラムを書きなさい.→より詳しい入力等はScrapboxを参照
K3Python2018 OOP-03 FULL
K3Python2018 OOP-03 FULL
K3Python2018 OOP-03 FULL
カプセル化:隠す機能
端的にいうと変数や関数を隠す機能
変数や関数の名前の最初2文字を_(アンダースコア)にすると隠せる
__hp
__check(self, x, y): など
__init__もこれにより隠された機能の一つ
演習問題・座標上の点を表す
xy平面上の点を表す、Pointクラスを作る
★メンバ変数
x, y: 座標
★メンバ関数
distance(p): 引数にPointインスタンスを取り、その点との距離を返す
nearest(lis): 引数にPointインスタンスのリストを取り、そのリスト
の中で最も近いPointを返す
余裕があったらturtle,tkなどで描画させる関数も作ってみる
継承
「さん」!「様」!「殿」!(o・∇・o) それは敬称(天゚∀゚)
似たクラスを作りたい
たとえば……
Playerクラスに対して敵を表すEnemyクラス
Player:Atk, Def, Spd, Lv, Exp
Enemy :Atk, Def, Spd
一緒のところが多い!!
こんなときに継承しよう!!
親クラスと子クラス
あるクラスを継承したものをそのクラスの子クラスと言う
逆に元のクラスのことを親クラスと言う
子クラスは基本的に親のコピー!!
親と差異のあるところを追加で書き足せる
変数の削除などは基本的に出来ない
今回の場合
Characterクラスというクラスを作る
共通する要素を含ませる
Characterクラスを継承
→PlayerクラスとEnemyクラスを作成する
class Character:
def __init__(self, _atk, _de, _spd, _hp):
self.atk = _atk
self.de = _de
...
class Player(Character):
def __init__(self, _atk, _de, _spd, _hp, _lv, _exp):
super().__init__(_atk, _de, _spd, _hp)
self.lv = _lv
self.exp = _exp
【練習】実際に実装してみよう
EnemyクラスとPlayerクラスを実際に実装してみる
継承されたクラスの動きを確認できればOK
パラメータは各自お任せ。
技を複数持たせるなど、適度に工夫しても良い
オーバーライド
親クラスですでに定義している関数を、子クラスで改めて定義し直すこと
イメージとしては上書きされるイメージ
定義の仕方は通常の関数と同じように子クラスで定義
演習・図形クラス
Figureクラス(親クラス)を作り、その後でTriangle, Circle,
Rectangleなどのクラスも作ってみる
class Figure:
def area(self):
# 面積を求める関数
pass
def include(self, point):
pass# point引数を取り、その点が図形内にあるかを
True/Falseで返す
class Circle:
def __init__(self, _x, _y, _r):
self.x, self.y = _x, _y # 中心座標
self.r = _r # 半径
def area(self):
# オーバーライドして面積を返す
def include(self, point):
# オーバーライドしてpointが中にあるかを返す
カードゲームを作ろうプロジェクト②
トランプ~~(o・∇・o) ウノとかでもいいよ(天゚∀゚)
演習問題(クラス・前回の続き)
前回作ったCardクラス、Deckクラスの他に、Playerクラスを作り、
手札を作る
Playerクラスはプレイヤーのステータスを表す。各々が作るゲー
ムに依って以下のような変数などを持つ。
Hands:手札
Point:現在の点数
Coin:持っている金
プログラムを組む前にどんな構造にするか考えてみると良いかも。
例:ブラックジャックの場合
Playerクラス
変数
Hands:手札
Coins:持っているコイン
Bet:掛けたお金
などなど・・・
関数
draw:カードを追加で一枚引く
betting:掛け金を決める
などなど・・・

More Related Content

Recently uploaded

Recently uploaded (8)

LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアルLoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
LoRaWAN無位置ロープ式水漏れセンサーWL03A 日本語マニュアル
 
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
Hyperledger Fabricコミュニティ活動体験& Hyperledger Fabric最新状況ご紹介
 
Keywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltdKeywordmap overview material/CINC.co.ltd
Keywordmap overview material/CINC.co.ltd
 
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
2024年5月17日 先駆的科学計算フォーラム2024 機械学習を用いた新たなゲーム体験の創出の応用
 
情報を表現するときのポイント
情報を表現するときのポイント情報を表現するときのポイント
情報を表現するときのポイント
 
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdfネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
ネットワーク可視化 振る舞い検知(NDR)ご紹介_キンドリル202405.pdf
 
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイルLoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
LoRaWAN無位置ロープ型水漏れセンサー WL03A-LB/LSカタログ ファイル
 
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その1) 2024/05/17の勉強会で発表されたものです。
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

K3Python2018 OOP-03 FULL