SlideShare una empresa de Scribd logo
1 de 31
Gramin(ぐらみ
ん)
JavaJava の抽象クラス・の抽象クラス・
インタフェース・無インタフェース・無
名クラスを理解しよ名クラスを理解しよ
うう
Java の継承を深く理解しよ
う抽象クラス、インタフェース、無名クラスの関係を理解しよう
抽象クラス
インタフェース
無名クラス
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
??
資料の紹介
小学生〜中学生を対象としたプログラミング教室 Gramin
この資料はスマートフォンアプリ開発クラス向けに作成
Java 基本構文は理解しクラスの理解に苦しむ方が対象
本来は授業中にこの資料に近いものを元に口頭で説明
抽象クラス・インタフェースの書き方は知っている方が対象
作成した背景
なぜインタフェースがあるか理解してもらえない。。。
抽象クラス・インタフェースがごっちゃになっている。。。
Android では無名クラスをよく使うのでそれも説明したい
資料は構造化(目次とか)すると内容が大きくなり
口頭で説明しにくくなるのでフラットにしています
哺乳類がいます
Mammal(マムル)
哺乳類にはネズミがいます
Rat(ラット)
ネズミは哺乳類です
Rat(ラット)
”ネズミは哺乳類を “継承 しています
Mammal(マムル)
継承
継承関係はこんな矢印で示す(親に矢印が向く)
哺乳類には馬もいます
Horse(馬)
Mammal(マムル)
継承
”馬は哺乳類を “継承 しています
哺乳類にはサルもいます
Ape(サル)
Mammal(マムル)
継承
”サルは哺乳類を “継承 しています
今の時点の継承関係
Mammal(哺乳類)
Rat(ネズミ) Horse(馬) Ape(サル)
哺乳類は体毛の数を持ちます
” ” ”オブジェクト志向で 体毛の数 は “属性 として表現できる
Mammal(哺乳類)
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数
継承すると属性も継承される
哺乳類は実際にいる生物?
“ ”哺乳類 という生
き物は存在する?new?? new でき
る?
“ ”哺乳類 という単位で new する
とおかしくなる(実際に存在せ
” ”ず 分類 でしかないから)
じゃあ、抽象クラス(abstract)
にしよう!
今の時点の継承関係
Mammal(哺乳類)
abstract
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数
属性:前歯の寿命 属性:走る速度 関数:木に登る()
ついでにネズミ・馬・サルに属性と関数をつけてみました
爬虫類もいます
Reptilia(レプティリア)
爬虫類にはトカゲがいます
”トカゲは爬虫類を “継承 しています
Lizard(トカゲ)
Reptilia(爬虫類)
継承
爬虫類は実際にいる生物?
new?? “ ”爬虫類 という生
き物は存在する?
new でき
る?
“ ”爬虫類 という単位で new する
とおかしくなる(実際に存在せ
” ”ず 分類 でしかないから)
じゃあ、爬虫類も抽象クラス
(abstract)にしよう!
今の時点の継承関係
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
爬虫類にウロコの硬さ属性とトカゲに噛み付くもつけてみまし
た
だんだん大きくなってきた。。。
トカゲもネズミも馬も4本足
です
こんな時どうやって4本足生物の継承関係を表現する?
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
FourLegs(4本足生物)
最近のオブジェクト志向言語では多重継承は禁止
C++ではOK
4本足はインタフェースにし
ます
これなら別の継承構造を持つクラスもひとまとめにできる
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数
FourLegs(4本足生物)
interface
属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
インタフェースはいくつでも実装可能
4本足生物は前足で蹴ります
中身はよく分からなくても「前足で蹴る」生物がいることが分
かる
関数:前足で蹴る()
FourLegs(4本足生物)
4本足生物の使い道は?
前足で蹴ることを知っているだけでも敵を攻撃することはでき
ます
誰が中身か知らないけど
蹴ってくれます
?
?
?
敵を攻撃する目的はそれ
だけでも十分果たしてい
ます
今の時点の継承関係
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
属性:体毛の数
FourLegs(4本足生物)
interface 関数:前足で蹴る()
属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
サルは人に進化しました
Ape(サル)
Mammal(マムル)
継承
”人はサルを “継承 しています
Human(人)
継承
今の時点の継承関係
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
Human(人)
属性:体毛の数
FourLegs(4本足生物)
interface 関数:前足で蹴る()
属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
関数:考える()
4本足生物は知らないとダ
メ?
前足で蹴るってことだけ知っていればいいんじゃない?
Java ”の場合 前足で蹴る“
関数の中身だけ定義して
あれば使える
これが無名クラス
普通のインタフェースの実装
4本足生物クラス
ネズミクラ
ス
インタフェースの実装 implements を使って関数を Override す
る
4本足生物は「前
足で蹴る」生物
継承したクラスで
は必ず「前足で蹴
る」関数をもって
いなければならな
い(Override)
インタフェースを使う時
インタフェースの実装 implements を使って関数を Override す
る
new した中身(インスタンス)が何
であろうとも
fourLegs.kickWithForeFoot();
を することには わら関数 実行 変
ない
インタフェースは「接続部分」の意
味KickWithForeFoot … 前足で蹴る
インタフェースを毎回作るのは
大変
手っ取り早く前足で蹴りたいだけの時もある
作ったクラスを
new する
インタフェースを実
装したクラスを作る
1 2
実行する
3
特に1の作業はファイルも分けたりと面倒くさい。。。
無名関数に話しを戻す
と。。。
前足で蹴るだけならクラス名なんて無くてもいいんじゃない?
インタフェースを実装した
無名を作って変数に格納
1 実行する
作業が結構減って単純になる
2
最終的な継承関係
Mammal(哺乳類)
abstract
Reptilia(爬虫類)
abstract
Lizard(トカゲ)
Rat(ネズミ) Horse(馬) Ape(サル)
Human(人)
無名クラス
属性:体毛の数
FourLegs(4本足生物)
interface 関数:前足で蹴る()
属性:ウロコの硬さ
関数:噛み付く()
属性:前歯の寿命 属性:走る速度 属性:木に登る
関数:考える()
終わり
ありがとうございました

Más contenido relacionado

Último

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024koheioishi1
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationYukiTerazawa
 
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ssusere0a682
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2Tokyo Institute of Technology
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptyuitoakatsukijp
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ssusere0a682
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料Takayuki Itoh
 

Último (7)

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScript
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 

Destacado

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 
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...Applitools
 
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 WorkGetSmarter
 
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...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Destacado (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
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
 

Java の抽象クラス・インタフェース・無名クラスを理解しよう