SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Java&OO道場(StartUp編
   その2)(全2回)のメモ

         2012-06-09(土) by munch
この勉強会の目的は・・・



 ・オブジェクト・クラス・インスタンス、について
  説明ができない。 
 ・継承とインタフェースの使い分けができない。
 ・そもそもインタフェースって何?何が嬉しいの?
 ・デザインパターンの良さがわからない。

⇒ の、様な疑問に対して「自分なりの解答(もしくはヒント)を
   持ち帰ってもらう、でしたが、どうでしょう?
本日のキーワード


「 お 笑 い」
 ではなく、、、
今回も、


  シンプル」
 「シンプル」
  Surtout faites simple
(出来る限り単純に作りなさい)

エスコフィエの料理の哲学より
軽く復習も兼ねて
「オブジェクト指向」でよく出てくる考え方
1.OOA、OOD、OOP      長くなるので、詳細省略
2.オブジェクト(データと操作)
3.メッセージパッシング             前回やったので、
4.カプセル化(情報隠蔽、部品化、再利用)     詳細は省略
5.クラス・インスタンス
6.階層構造
7.継承
8.ポリモーフィズム(多相性、多様性、多態性)

                       今回のキモ
その前に  軽くJavaの文法の復習を(その1)
Java 言語において「データ型」と呼ばれるものは3種類存在する

 基本データ型 プリミティブ型
 基本データ型(プリミティブ型)
   データ
   ・・・boolean, char, byte, short, int, long, float, double の
      8種類

 参照型(リファレンス型
 参照型(リファレンス型)
   ・・・String、Date 等のJavaのライブラリに含まれたものや
      それぞれの開発者が作成したクラス

 配列型                                       これには次のシートの
  ・・・若干特殊な参照型                               3種類が存在する
その前に 軽くJavaの文法の復習を(その2)
    Java 言語において「クラス」と呼ばれるものは3種類存在する
イ
ン    高   Interface(インタフェース)
                  (インタフェース)
ス
タ     
ン           ・・・定数と抽象メソッドのみを持つ
ス     
生
成    抽
不
可
     象   AbstractClass(抽象クラス)
                      (抽象クラス)
                         クラス
能    度
 
           ・・・定数と変数、抽象メソッド・具象メソッドを持つ
      
 
      
可     
能    低   Class(クラス or 具象クラス)
              (       具象クラス
                        クラス)
            ・・・定数と変数、具象メソッドのみを持つ
(※)「抽象メソッド」・・・「実装を持たない(≠実装が空)メソッド」
                  持たない
さっきの内容を表にすると・・・

                 フィールド          メソッド
                 定数   変数   抽象メソッド   具象メソッド

Interface
(インタフェース)
 インタフェース)
                 ○    ×     ○          ×

AbstractClass
                 ○    ○     ○          ○
(抽象クラス)
 抽象クラス)
   クラス


Class
(クラス or 具象クラス)
        具象クラス
          クラス)
                 ○    ○     ×          ○
ここらで、一発軽い
 コーディングを
そうです、継承についてです
「継承」についてのコーディング(その1)
●3つのクラス「Oya」「Ko」「KeisyoMain」があります。
  ⇒ 「http://bit.ly/L5Zs3y」にコードがあります。

問1.「KeisyoMain」クラスの1つ目のコメントアウトを解除して下さい
   ⇒ 「Ko」クラスが「Oya」クラスを継承するように修正して下さい

                            Oya
                       printOya()
                       printZaisan()
                                       継承させる
   KeisyoMain               Ko
                uses
   main()              printKo()
                       printZaisan()
「継承」についてのコーディング(その2)
問2.「KeisyoMain」クラスの2つ目のコメントアウトを解除して
   実行結果を確認してみて下さい。
   また、「Ko」でオーバーライドされた「printZaisan()」の結果と、
   「Oya」の「printZaisan()」の結果を確認してみて下さい


                          Oya
                     printOya()
  実行結果の
                     printZaisan()   super.printZaisa
    確認
                                     n() というコード
                                     を追加してみま
                                          しょう
 KeisyoMain               Ko
              uses
main()               printKo()
                     printZaisan()
「継承」についてのコーディング(その3)
問3.「Oya」クラスを型として、「Ko」のインスタンスを作成した場合の
   実行結果を確認してみて下さい
   ⇒「Ko」クラス内に存在する「printKo()」は使用できたでしょうか?



                            Oya
                       printOya()
                       printZaisan()

                                       実行結果の
                                        確認
                uses
   KeisyoMain               Ko
   main()              printKo()
                       printZaisan()
「継承」についてのコーディング(その4)
問4.「Oya」クラスを型として、「Ko」のインスタンスを作成した物を、
   「Ko」クラスの型へ「ダウンキャスト」した時の実行結果を
   確認してみて下さい
   ⇒「Ko」クラス内に存在する「printKo()」は使用できたでしょうか?


                            Oya
                       printOya()
                       printZaisan()

                                       実行結果の
                                        確認
                uses
   KeisyoMain               Ko
   main()              printKo()
                       printZaisan()
「継承」についてのコーディング(番外編)
●「Ko」に対して、2つ以上の親クラスを継承させようとするとどうなるでしょう?
                      Oya1                            Oya2
                  printOya()                      printOya()
                  printZaisan()                   printZaisan()


                                       Ko
                                  printKo()
                                  printZaisan()


●「Oya1」が「Oya2」を継承し、「Oya2」が「Oya3」を継承するような関係の
  場合、「Oya1」を継承した「Ko」の動きはどうなるでしょう?

      Oya3                  Oya2                       Oya1
  printOya3()           printOya2()                printOya()
  printZaisan()         printZaisan()              printZaisan()

                                                                        Ko
                                                                   printKo()
       後は、各メソッドのアクセス修飾子を                                           printZaisan()
        変えてみたりしても面白いかも
「継承」のポイント
●「Oya」(親クラス=スーパークラス)と「Ko」(子クラス=サブクラス)の
  関係は理解できましたか?

●継承されたときの子クラス(サブクラス)の利用法についてはどうでしょう?
  ⇒ 親クラスのメソッドが使えます(アクセス修飾子については要注意)
  ⇒ 親クラスのメソッドが子クラスでオーバーライドされる場合があります。

●Javaでは「単一継承」しかできません

●アップキャスト、ダウンキャストした時の動きについてはどうでしょう?

            継承には弱点がある
               には弱点
⇒ 以上を踏まえて、「継承には弱点がある」という話を少々。
   (「継承」を考える前に、「委譲」の可能性を検討する)
「継承」のおまけ(単一継承と多重継承)
          単一継承(Java とか)                          多重継承(C++とか)

                Oya                                     Oya1
           printOya()                               printOya()
           printZaisan()                            printZaisan()


     Ko1               Ko2
printKo()         printKo()                    Oya1              Oya2
printZaisan()     printZaisan()            printOya()        printOya()
                                           printZaisan()     printZaisan()

               Ko3              Ko4                       Ko
          printKo()        printKo()                 printKo()
          printZaisan()    printZaisan()             printZaisan()



    (※)「親」「子」というのはそれぞれの相対
      的な関係において定義されます。                                         ダイヤモンド継承問題
ここらで、もう一発軽い
  コーディングを
「インタフェース」についてのコーディング(その1)
●5つのクラス(及びインタフェース)「Drivable」「Racable」
  「Cycle」 「Train」「TestIFMain」があります。
  ⇒ 「http://bit.ly/KCuFPj」にコードがあります。

問1.「TestIFMain」クラスの1つ目のコメントアウトを解除して下さい
   ⇒ 「Cycle」クラスが「Drivable」インタフェースを実装するように
     修正して下さい
                    <Drivable>    <Racable>
                    start()      start()
         実装させる

TestIFMain              Cycle              Train
             uses
main()              start()          start()
                    turn()           stop()
「インタフェース」についてのコーディング(その2)
問2.「Cycle」クラスについて、「Racable」インタフェースも
    実装するように修正して下さい




                    <Drivable>    <Racable>
                    start()      start()
         実装させる

TestIFMain              Cycle              Train
             uses
main()              start()          start()
                    turn()           stop()
「インタフェース」についてのコーディング(その3)
問3.「Cycle」クラスと「Train」クラスについて、実行結果を確認して下さい




                    <Drivable>
                    start()

             uses
TestIFMain              Cycle         Train
main()              start()      start()
                    turn()       stop()
以上を踏まえて、
いよいよ今回の一番の
 メインディッシュを
白板で説明したネタのメモです
                       <Comedian>                <Writer>
                                                                       uses
                       performance()          write()

                                                                   Publisher
                                                                  main()
      99
                                       Mr.M
performance()
                                 performance()
                uses                                        Mr.K
                                 write()
      Hall                       produce()              write()
    main()
白板で説明したネタのメモです
インタフェース利用時
インタフェース利用時   インタフェース非
             インタフェース非利用時
「インタフェース」のポイント
●インタフェースについては理解できましたか?

●ココでも「シンプル」「疎結合」という単語が出てきました

●継承と比較してどういう違いがあるでしょうか?

●「暗黙の期待」についてはどうでしょうか?

⇒ 以上を踏まえて、「デザインパターン」に進みたいんですが、
   その前に「例外」についてのお話を少々。

  一つだけ。「抽象クラス」は、まだ登場していませんがそれを利用すれば
  何が嬉しいのでしょうか?(彼はどんな場面で活躍できるのでしょうか?)
例外について(階層構造)
Object

         Throwable
                Error               非チェック例外


                Exception
                            RuntimeException
 チェック例外                     その他
                            その他のException

  (参考)「Java言語プログラミングレッスン 下巻」(結城浩著)
例外について(処理方法)
●「try~catch」して処理する

●「throws」で呼び出し元に処理を任せる

           例外は例外発生時のみの処理とする
                      のみの処理
⇒ いずれにせよ「例外は例外発生時のみの処理とする」のが重要!
   ∵例外発生時以外の処理を含めると、「ホントに例外か?」という状況に。。。
    (そもそも「例外」なんて無いに越したことは無い)

    「例外による脱出口が沢山ある」=「メソッドからの返り値が複数ある」
    という事に繋がりかねず、設計が複雑になってしまう(シンプルさ、重要!)

   (個人的経験としては「例外」についてのポリシーがイケてないシステムは、
    大概その他もイケてないので要注意!)
     (参考)「Java言語プログラミングレッスン 下巻」
          「達人プログラマー」「Javaの格言」
白板で説明したネタのメモです
「はがき」を書くためのクラスを作ってみる
 はがき」  くためのクラスを
           クラス




抽象化する

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
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
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
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...
 

20120609 java oo道場(ネタのメモ)