SlideShare ist ein Scribd-Unternehmen logo
1 von 38
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() ,
 請大家回去自己做做看。
謝謝大家。
歡迎提出問題。

Weitere ähnliche Inhalte

Andere mochten auch

HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015Mary St Mary
 
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applicationsimacat .
 
Informatica jeka
Informatica jekaInformatica jeka
Informatica jekaGaznatiiTha
 
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 reasonableAlexandria University, Egypt
 

Andere mochten auch (20)

Fra campingvogn til kulturarvstjeneste
Fra campingvogn til kulturarvstjenesteFra campingvogn til kulturarvstjeneste
Fra campingvogn til kulturarvstjeneste
 
HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015HeroinFentanyl ODs Feb2015
HeroinFentanyl ODs Feb2015
 
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
 
Nanna Bjerregaard Pedersen - d-mannitol
Nanna Bjerregaard Pedersen -  d-mannitolNanna Bjerregaard Pedersen -  d-mannitol
Nanna Bjerregaard Pedersen - d-mannitol
 
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
 
Mette Madsen - køge bugt revisited
Mette Madsen -  køge bugt revisitedMette Madsen -  køge bugt revisited
Mette Madsen - køge bugt revisited
 
Morten Søvsø - grave og bebyggelse i emporiet ribe
Morten Søvsø - grave og bebyggelse i emporiet ribeMorten Søvsø - grave og bebyggelse i emporiet ribe
Morten Søvsø - grave og bebyggelse i emporiet ribe
 
Torben Trier Christiansen, Stormandsgården fundet
Torben Trier Christiansen, Stormandsgården fundetTorben Trier Christiansen, Stormandsgården fundet
Torben Trier Christiansen, Stormandsgården fundet
 
Lotte Sparrevohn
Lotte SparrevohnLotte Sparrevohn
Lotte Sparrevohn
 
Title Sequences
Title  SequencesTitle  Sequences
Title Sequences
 
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applications
 
Future of CRM
Future of CRM Future of CRM
Future of CRM
 
Henrik Sell - tilbage til istiden
Henrik Sell -  tilbage til istidenHenrik Sell -  tilbage til istiden
Henrik Sell - tilbage til istiden
 
Bodil Holstein - foto i stedet for intarsia
Bodil Holstein -  foto i stedet for intarsiaBodil Holstein -  foto i stedet for intarsia
Bodil Holstein - foto i stedet for intarsia
 
Informatica jeka
Informatica jekaInformatica jeka
Informatica jeka
 
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
 
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
 
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
 
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
 
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
 

Ähnlich wie Solitaire with Greenfoot #2

Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1imacat .
 
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 01imacat .
 
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3imacat .
 
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二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 LusterJason Chung
 

Ähnlich wie Solitaire with Greenfoot #2 (8)

Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
 
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
 
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
 
由一个简单的程序谈起――之二
由一个简单的程序谈起――之二由一个简单的程序谈起――之二
由一个简单的程序谈起――之二
 
由一个简单的程序谈起――之三(精华)
由一个简单的程序谈起――之三(精华)由一个简单的程序谈起――之三(精华)
由一个简单的程序谈起――之三(精华)
 
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
 
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
 
AngularJS training in Luster
AngularJS training in LusterAngularJS training in Luster
AngularJS training in Luster
 

Mehr von imacat .

A Room of WikiWomen's Own
A Room of WikiWomen's OwnA Room of WikiWomen's Own
A Room of WikiWomen's Ownimacat .
 
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 Pythonimacat .
 
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docsimacat .
 
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 Diversityimacat .
 
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4imacat .
 
OpenOffice, Open Business
OpenOffice, Open BusinessOpenOffice, Open Business
OpenOffice, Open Businessimacat .
 
Multimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice CalcMultimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice Calcimacat .
 
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 2012imacat .
 
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calcimacat .
 
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4imacat .
 
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1imacat .
 
Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4imacat .
 
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4imacat .
 
OpenOffice UNO Application on Android
OpenOffice UNO Application on AndroidOpenOffice UNO Application on Android
OpenOffice UNO Application on Androidimacat .
 
OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandboximacat .
 
Mailing Lists and IRC
Mailing Lists and IRCMailing Lists and IRC
Mailing Lists and IRCimacat .
 
GNU Build System
GNU Build SystemGNU Build System
GNU Build Systemimacat .
 
patch和diff
patch和diffpatch和diff
patch和diffimacat .
 
OpenOffice.org UNO Magic
OpenOffice.org UNO MagicOpenOffice.org UNO Magic
OpenOffice.org UNO Magicimacat .
 

Mehr von 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
 
Office寶可夢GO IV計算機
Office寶可夢GO IV計算機Office寶可夢GO IV計算機
Office寶可夢GO IV計算機
 
OpenOffice Application with Python
OpenOffice Application with PythonOpenOffice Application with Python
OpenOffice Application with Python
 
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs
 
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
 
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4
 
OpenOffice, Open Business
OpenOffice, Open BusinessOpenOffice, Open Business
OpenOffice, Open Business
 
Multimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice CalcMultimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice Calc
 
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
 
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
 
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
 
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
 
Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4
 
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4
 
OpenOffice UNO Application on Android
OpenOffice UNO Application on AndroidOpenOffice UNO Application on Android
OpenOffice UNO Application on Android
 
OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
 
Mailing Lists and IRC
Mailing Lists and IRCMailing Lists and IRC
Mailing Lists and IRC
 
GNU Build System
GNU Build SystemGNU Build System
GNU Build System
 
patch和diff
patch和diffpatch和diff
patch和diff
 
OpenOffice.org UNO Magic
OpenOffice.org UNO MagicOpenOffice.org UNO Magic
OpenOffice.org UNO Magic
 

Solitaire with Greenfoot #2