SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
接龍遊戲
由上而下的物件導向程
  式設計(下)

      依瑪貓/楊士青
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 card) 把牌放到未翻開的
 牌堆。
重複做到所有的牌都還回去為止。
步驟二:方法內容實作
重新翻牌 returnAllCards()



   歡迎大家自己實作看看,
     測試自己的實力!
步驟二:方法內容實作
          重新翻牌 returnAllCards()
/**
 * 已翻開的撲克牌疊。
 */
public class FlippedPile extends Pile
{
      /**
       * 重新翻牌。把撲克牌全部退回未翻開的牌疊。
       */
      public void returnAllCards() {
            Table table = (Table) getWorld();
            Card card = takeTopCard();
            while (card != null) {
                card.turnFaceDown();
                table.unflippedPile.addCard(card);
                card = takeTopCard();
            }
      }
}
試著執行看看。
剩下的,大家可以試著自己做做看。
謝謝大家。
歡迎提出問題。

Contenu connexe

En vedette

OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandboximacat .
 
Real World Social CRM use cases
Real World Social CRM use casesReal World Social CRM use cases
Real World Social CRM use casesAtcore Systems
 
Atcore Connect - SugarCRM Call Logging by the Numbers
Atcore Connect - SugarCRM Call Logging by the NumbersAtcore Connect - SugarCRM Call Logging by the Numbers
Atcore Connect - SugarCRM Call Logging by the NumbersAtcore Systems
 
Video para ingles
Video para inglesVideo para ingles
Video para ingles7755
 
Your Legacy: Impact or influence
Your Legacy: Impact or influenceYour Legacy: Impact or influence
Your Legacy: Impact or influencePaul Johnson
 
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calcimacat .
 
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 .
 

En vedette (20)

Katrine Segel - konservering af poul gernes skulptur
Katrine Segel -  konservering af poul gernes skulpturKatrine Segel -  konservering af poul gernes skulptur
Katrine Segel - konservering af poul gernes skulptur
 
Anders Hartvig - petersborg
Anders Hartvig  - petersborgAnders Hartvig  - petersborg
Anders Hartvig - petersborg
 
OpenOffice.org Magic Sandbox
OpenOffice.org Magic SandboxOpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
 
Real World Social CRM use cases
Real World Social CRM use casesReal World Social CRM use cases
Real World Social CRM use cases
 
Atcore Connect - SugarCRM Call Logging by the Numbers
Atcore Connect - SugarCRM Call Logging by the NumbersAtcore Connect - SugarCRM Call Logging by the Numbers
Atcore Connect - SugarCRM Call Logging by the Numbers
 
45 Kirsten Prangsgaard, Nyt på fyn
45 Kirsten Prangsgaard, Nyt på fyn45 Kirsten Prangsgaard, Nyt på fyn
45 Kirsten Prangsgaard, Nyt på fyn
 
Video para ingles
Video para inglesVideo para ingles
Video para ingles
 
MCH Data to Action
MCH Data to ActionMCH Data to Action
MCH Data to Action
 
Your Legacy: Impact or influence
Your Legacy: Impact or influenceYour Legacy: Impact or influence
Your Legacy: Impact or influence
 
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice CalcMosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
 
Democraciasinpartidos
DemocraciasinpartidosDemocraciasinpartidos
Democraciasinpartidos
 
lennart madsen
lennart madsenlennart madsen
lennart madsen
 
Ulla Mannering
Ulla ManneringUlla Mannering
Ulla Mannering
 
Göran björnberg - Why Museums?
Göran björnberg - Why Museums?Göran björnberg - Why Museums?
Göran björnberg - Why Museums?
 
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
 
Peter Vang Petersen
Peter Vang PetersenPeter Vang Petersen
Peter Vang Petersen
 
Jens Aage Søndergaard - kortlægning af de landbrugshistoriske samlinger på d...
Jens Aage Søndergaard -  kortlægning af de landbrugshistoriske samlinger på d...Jens Aage Søndergaard -  kortlægning af de landbrugshistoriske samlinger på d...
Jens Aage Søndergaard - kortlægning af de landbrugshistoriske samlinger på d...
 
Afib slides-focused-update-2012
Afib slides-focused-update-2012Afib slides-focused-update-2012
Afib slides-focused-update-2012
 
Aoife Daly - bolværkerne ved metroudgravningen
Aoife Daly -  bolværkerne ved metroudgravningenAoife Daly -  bolværkerne ved metroudgravningen
Aoife Daly - bolværkerne ved metroudgravningen
 
Louise Karlskov Skyggebjerg - har du talt med dine genstande i dag
Louise Karlskov Skyggebjerg -  har du talt med dine genstande i dagLouise Karlskov Skyggebjerg -  har du talt med dine genstande i dag
Louise Karlskov Skyggebjerg - har du talt med dine genstande i dag
 

Plus de 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 .
 
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applicationsimacat .
 
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 .
 
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 #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3imacat .
 
Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1imacat .
 
Solitaire with Greenfoot #4
Solitaire with Greenfoot #4Solitaire with Greenfoot #4
Solitaire with Greenfoot #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 .
 
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 .
 

Plus de 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計算機
 
Crossing Office Applications
Crossing Office ApplicationsCrossing Office Applications
Crossing Office Applications
 
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
 
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 #3
Solitaire with Greenfoot #3Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
 
Solitaire with Greenfoot #1
Solitaire with Greenfoot #1Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
 
Solitaire with Greenfoot #4
Solitaire with Greenfoot #4Solitaire with Greenfoot #4
Solitaire with Greenfoot #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
 
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
 

Object-Oriented Programming Design with Greenfoot 02