狠狠撸

狠狠撸Share a Scribd company logo
Greenfoot 接龍(二)
由上而下的物件導向程
    式設計(下)

      依瑪貓/楊士青
imacat@mail.imacat.idv.tw
       2012/6/15
「接龍遊戲—由上而下的物件導向程式設計」簡報由 依瑪貓╱楊士青 製作,
 以 創用CC Attribution-ShareAlike 3.0 Unported 授權條款 釋出。
自我介紹
依瑪貓╱楊士青
臺灣師範大學資訊教育研究所碩一研究生
二、設計方法 (Method)
類別、物件建立好了以後,
要怎麼樣才能讓程式動起來?
玩接龍的時候,
撲克牌是怎麼動的呢?
扑克牌是怎麼动的?
还没翻的牌叠
已翻开的牌叠
暂放区的牌叠
归整区的牌叠
扑克牌是怎麼动的?
还没翻的牌叠    暂放区的牌叠
 翻牌        移到暫放區其他疊
翻開來的牌疊     移到歸整區
 重新翻牌     归整区的牌叠
 移到暫放區     移到暫放區
 移到歸整區     移到歸整區其他疊
步驟一:建立方法
还没翻的牌叠    class UnflippedPile
          {
 翻牌           public void flipNextCard() {
              }
          }
步驟一:建立方法
翻開來的牌疊
         class FlippedPile
         {

 重新翻牌        public void returnAllCards() {
             }
 移到暫放區       public void moveToWorking() {

 移到歸整區       }
             public void moveToResult() {
             }
         }
步驟一:建立方法
暂放区的牌叠
            class WorkingPile
            {

 移到暫放區其他疊       public void moveToWorking() {
                }
 移到歸整區          public void moveToResult() {
                }
            }
步驟一:建立方法
归整区的牌叠
            class ResultPile
            {

 移到暫放區          public void moveToWorking() {
                }
 移到歸整區其他疊       public void moveToResult() {
                }
            }
步驟一:建立方法



請先建立沒有內容的空方法。
   不需要填上内容。
步驟一:建立方法



 物件要做什麼動作,
我們就建立什麼方法。
步驟一:建立方法
每個物體的動作,都對應到我們建立的一個
 method 。
 物件導向程式設計,把我們理解的抽象物件行為
  ,直接對應到 method ,寫成 method 。
方法建好後,
我們就來開始實作方法裏的程式碼。
步骤二:方法内容实作
    翻牌 flipNextCard()
用 takeTopCard() 抽出最上面的撲克牌。
用 turnFaceUp() 翻正面。
用 addCard(Card card) 把牌放到已翻開的
 牌堆。
步骤二:方法内容实作
       強制型別轉換
Table table = (Table) getWorld()
getWorld() 是 UnflippedPile 繼承自 Actor
 的方法,傳回值型態是 World 。
步骤二:方法内容实作
       強制型別轉換
getWorld() 是 UnflippedPile 繼承自 Actor
的方法,傳回值型態是 World 。
我們要拿 Table 的 flippedPile 來
  addCard , flippedPile 屬於 Table ,而
  不是 World 。
Java 編譯認定 World 沒有 flippedPile ,只
  有 Table 才有 flippedPile 。
步骤二:方法内容实作
      強制型別轉換
解決方法:強制型別轉換 type casting 。
(Table) getWorld()
 和 C 語言的強制型別轉換一樣。
   float pi = 3.1415926;
   int p = (int) pi;
步骤二:方法内容实作
       強制型別轉換
子類別轉到父類別,因為子類別本來就屬於
父類別,不需強制型別轉換。
 World world = table;
 card.pile = unflippedPile;
 Pile pile = unflippedPile;
步骤二:方法内容实作
       強制型別轉換
父類別轉到子類別,才需強制型別轉換。
 Table table = (Table) getWorld();
步骤二:方法内容实作
      強制型別轉換
只有父類別可以轉到子類別,不相關的類別
間不能轉。
 以下都是錯誤範例:
  FlippedPile pile = (FlippedPile) unflippedPile;
  Card card = (Card) pile;
  Table table = (Table) card;
步骤二:方法内容实作
     強制型別轉換
強制型別轉換有危險性,教科書教很多。
 但因為我們已知這個 World 就是 Table ,這是
  程式設計者已知的現實,所以無所謂。
步骤二:方法内容实作
      強制型別轉換
因為 Actor 不知道 Table 的存在,由 Actor
繼承來的 getWorld() ,只可能回傳
World ,不可能直接回傳 Table 。
 所以父類別轉為子類別的強制型別轉換,是物件
  導向程式設計常見的技巧。
步骤二:方法内容实作
                翻牌 flipNextCard()
**
 * 未翻開的撲克牌疊。
 */
public class UnflippedPile extends Pile
{
      /**
       * 翻一張撲克牌。
       */
      public void flipNextCard()
      {
            Table table = (Table) getWorld();
            Card card = takeTopCard();
            card.turnFaceUp();
            table.flippedPile.addCard(card);
      }
}
试着执行看看。
步骤二:方法内容实作
  重新翻牌 returnAllCards()
用 takeTopCard() 抽出最上面的撲克牌。
用 turnFaceDown() 翻背面。
用 addCard(Card newCard) 把牌放到未翻
 開的牌堆。
重複做到所有的牌都還回去為止。
步骤二:方法内容实作
重新翻牌 returnAllCards()



   歡迎大家自己實作看看,
     測試自己的實力!
步骤二:方法内容实作
          重新翻牌 returnAllCards()
/**
 * 已翻開的撲克牌疊。
 */
public class FlippedPile extends Pile
{
      /**
       * 重新翻牌。把撲克牌全部退回未翻開的牌疊。
       */
      public void returnAllCards() {
            Table table = (Table) getWorld();
            while (getSize() > 0) {
                Card card = takeTopCard();
                card.turnFaceDown();
                table.getUnflippedPile().addCard(card);
            }
      }
}
试着执行看看。
如果你上面的練習都做得正確,
程式應該會長得像 solitaire-2 一樣。
習題



剩下的 moveToWorking() 、
  moveToResult() ,
 請大家回去自己做做看。
謝謝大家。
歡迎提出問題。

More Related Content

Viewers also liked (20)

Fra campingvogn til kulturarvstjeneste
Fra campingvogn til kulturarvstjenesteFra campingvogn til kulturarvstjeneste
Fra campingvogn til kulturarvstjeneste
Association of Danish Museums / Organisationen Danske Museer
?
HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015
Mary St Mary
?
Astrid Skou og Jannie Holm - spor efter udvinding og forarbejdning af jern
Astrid Skou og Jannie Holm -  spor efter udvinding og forarbejdning af jernAstrid Skou og Jannie Holm -  spor efter udvinding og forarbejdning af jern
Astrid Skou og Jannie Holm - spor efter udvinding og forarbejdning af jern
Association of Danish Museums / Organisationen Danske Museer
?
Theis Jensen - the use of 3 d photogrammetry for on-site recording
Theis Jensen - the use of 3 d photogrammetry for on-site recordingTheis Jensen - the use of 3 d photogrammetry for on-site recording
Theis Jensen - the use of 3 d photogrammetry for on-site recording
Association of Danish Museums / Organisationen Danske Museer
?
Title Sequences
Title  SequencesTitle  Sequences
Title Sequences
Dylan Conti
?
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applications
imacat .
?
Future of CRM
Future of CRM Future of CRM
Future of CRM
Atcore Systems
?
Informatica jekaInformatica jeka
Informatica jeka
GaznatiiTha
?
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonableSamir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Alexandria University, Egypt
?
Julie Sommerlund, Hvorfor skal museerne forske? Er det en opgave for Uni
Julie Sommerlund, Hvorfor skal museerne forske? Er det en opgave for UniJulie Sommerlund, Hvorfor skal museerne forske? Er det en opgave for Uni
Julie Sommerlund, Hvorfor skal museerne forske? Er det en opgave for Uni
Association of Danish Museums / Organisationen Danske Museer
?
Birgitte Faurh?j Olsen - orientering om nyfundne kalkmalerier
Birgitte Faurh?j Olsen  - orientering om nyfundne kalkmalerierBirgitte Faurh?j Olsen  - orientering om nyfundne kalkmalerier
Birgitte Faurh?j Olsen - orientering om nyfundne kalkmalerier
Association of Danish Museums / Organisationen Danske Museer
?
Louise Lund Johansen - om t?rvegrave i nordsj?lland
Louise Lund Johansen  - om t?rvegrave i nordsj?llandLouise Lund Johansen  - om t?rvegrave i nordsj?lland
Louise Lund Johansen - om t?rvegrave i nordsj?lland
Association of Danish Museums / Organisationen Danske Museer
?
20 bj+?rnar m+?ge_ahrensburglokalitet med faunalevn
20 bj+?rnar m+?ge_ahrensburglokalitet med faunalevn20 bj+?rnar m+?ge_ahrensburglokalitet med faunalevn
20 bj+?rnar m+?ge_ahrensburglokalitet med faunalevn
Association of Danish Museums / Organisationen Danske Museer
?
HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015
Mary St Mary
?
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applications
imacat .
?
Informatica jekaInformatica jeka
Informatica jeka
GaznatiiTha
?
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonableSamir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Alexandria University, Egypt
?

Similar to Solitaire with Greenfoot #2 (8)

Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
imacat .
?
Object-Oriented Programming Design with Greenfoot 01
Object-Oriented Programming Design with Greenfoot 01Object-Oriented Programming Design with Greenfoot 01
Object-Oriented Programming Design with Greenfoot 01
imacat .
?
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
imacat .
?
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二
yiditushe
?
由一个简单的程序谈起――之叁(精华)
由一个简单的程序谈起――之叁(精华)由一个简单的程序谈起――之叁(精华)
由一个简单的程序谈起――之叁(精华)
yiditushe
?
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
吳錫修 (ShyiShiou Wu)
?
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
吳錫修 (ShyiShiou Wu)
?
AngularJS training in Luster
AngularJS training in LusterAngularJS training in Luster
AngularJS training in Luster
Jason Chung
?
Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
imacat .
?
Object-Oriented Programming Design with Greenfoot 01
Object-Oriented Programming Design with Greenfoot 01Object-Oriented Programming Design with Greenfoot 01
Object-Oriented Programming Design with Greenfoot 01
imacat .
?
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
imacat .
?
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二
yiditushe
?
由一个简单的程序谈起――之叁(精华)
由一个简单的程序谈起――之叁(精华)由一个简单的程序谈起――之叁(精华)
由一个简单的程序谈起――之叁(精华)
yiditushe
?
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
吳錫修 (ShyiShiou Wu)
?
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
吳錫修 (ShyiShiou Wu)
?
AngularJS training in Luster
AngularJS training in LusterAngularJS training in Luster
AngularJS training in Luster
Jason Chung
?

More from imacat . (20)

A Room of WikiWomen's Own
A Room of WikiWomen's OwnA Room of WikiWomen's Own
A Room of WikiWomen's Own
imacat .
?
Office寶可夢GO IV計算機
Office寶可夢GO IV計算機Office寶可夢GO IV計算機
Office寶可夢GO IV計算機
imacat .
?
OpenOffice Application with Python
OpenOffice Application with PythonOpenOffice Application with Python
OpenOffice Application with Python
imacat .
?
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs
imacat .
?
More Girls – Creating a Community of Diversity
More Girls – Creating a Community of DiversityMore Girls – Creating a Community of Diversity
More Girls – Creating a Community of Diversity
imacat .
?
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4
imacat .
?
OpenOffice, Open Business
OpenOffice, Open BusinessOpenOffice, Open Business
OpenOffice, Open Business
imacat .
?
Multimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice CalcMultimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice Calc
imacat .
?
Welcome to Apache OpenOffice 3.4 COSCUP 2012
Welcome to Apache OpenOffice 3.4 COSCUP 2012Welcome to Apache OpenOffice 3.4 COSCUP 2012
Welcome to Apache OpenOffice 3.4 COSCUP 2012
imacat .
?
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
imacat .
?
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
imacat .
?
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
imacat .
?
Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4
imacat .
?
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4
imacat .
?
OpenOffice UNO Application on Android
OpenOffice UNO Application on AndroidOpenOffice UNO Application on Android
OpenOffice UNO Application on Android
imacat .
?
OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
imacat .
?
Mailing Lists and IRC
Mailing Lists and IRCMailing Lists and IRC
Mailing Lists and IRC
imacat .
?
GNU Build System
GNU Build SystemGNU Build System
GNU Build System
imacat .
?
辫补迟肠丑和诲颈蹿蹿
辫补迟肠丑和诲颈蹿蹿辫补迟肠丑和诲颈蹿蹿
辫补迟肠丑和诲颈蹿蹿
imacat .
?
OpenOffice.org UNO Magic
OpenOffice.org UNO MagicOpenOffice.org UNO Magic
OpenOffice.org UNO Magic
imacat .
?
A Room of WikiWomen's Own
A Room of WikiWomen's OwnA Room of WikiWomen's Own
A Room of WikiWomen's Own
imacat .
?
Office寶可夢GO IV計算機
Office寶可夢GO IV計算機Office寶可夢GO IV計算機
Office寶可夢GO IV計算機
imacat .
?
OpenOffice Application with Python
OpenOffice Application with PythonOpenOffice Application with Python
OpenOffice Application with Python
imacat .
?
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs
imacat .
?
More Girls – Creating a Community of Diversity
More Girls – Creating a Community of DiversityMore Girls – Creating a Community of Diversity
More Girls – Creating a Community of Diversity
imacat .
?
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4
imacat .
?
OpenOffice, Open Business
OpenOffice, Open BusinessOpenOffice, Open Business
OpenOffice, Open Business
imacat .
?
Multimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice CalcMultimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice Calc
imacat .
?
Welcome to Apache OpenOffice 3.4 COSCUP 2012
Welcome to Apache OpenOffice 3.4 COSCUP 2012Welcome to Apache OpenOffice 3.4 COSCUP 2012
Welcome to Apache OpenOffice 3.4 COSCUP 2012
imacat .
?
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
imacat .
?
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
imacat .
?
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
imacat .
?
Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4
imacat .
?
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4
imacat .
?
OpenOffice UNO Application on Android
OpenOffice UNO Application on AndroidOpenOffice UNO Application on Android
OpenOffice UNO Application on Android
imacat .
?
OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
imacat .
?
Mailing Lists and IRC
Mailing Lists and IRCMailing Lists and IRC
Mailing Lists and IRC
imacat .
?
GNU Build System
GNU Build SystemGNU Build System
GNU Build System
imacat .
?
辫补迟肠丑和诲颈蹿蹿
辫补迟肠丑和诲颈蹿蹿辫补迟肠丑和诲颈蹿蹿
辫补迟肠丑和诲颈蹿蹿
imacat .
?
OpenOffice.org UNO Magic
OpenOffice.org UNO MagicOpenOffice.org UNO Magic
OpenOffice.org UNO Magic
imacat .
?

Solitaire with Greenfoot #2