SlideShare 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

ODP
Object-Oriented Programming Design with Greenfoot 02
ODP
Solitaire with Greenfoot #4
DOCX
HeroinFentanyl ODs Feb2015
PPTX
Title Sequences
ODP
Crossing Office Applications
PDF
Future of CRM
DOCX
Informatica jeka
PDF
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable
Object-Oriented Programming Design with Greenfoot 02
Solitaire with Greenfoot #4
HeroinFentanyl ODs Feb2015
Title Sequences
Crossing Office Applications
Future of CRM
Informatica jeka
Samir rafla antiarrhythmic drug therapy in hf and af , what is reasonable

Similar to Solitaire with Greenfoot #2 (8)

ODP
Solitaire with Greenfoot #1
ODP
Object-Oriented Programming Design with Greenfoot 01
ODP
Solitaire with Greenfoot #3
PDF
由一个简单的程序谈起――之二
PDF
由一个简单的程序谈起――之三(精华)
PDF
Unity遊戲程式設計 - 2D運動與碰撞處理I
PDF
Unity遊戲程式設計 - 2D運動與碰撞處理I
PDF
AngularJS training in Luster
Solitaire with Greenfoot #1
Object-Oriented Programming Design with Greenfoot 01
Solitaire with Greenfoot #3
由一个简单的程序谈起――之二
由一个简单的程序谈起――之三(精华)
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
AngularJS training in Luster
Ad

More from imacat . (20)

ODP
A Room of WikiWomen's Own
ODP
Office寶可夢GO IV計算機
ODP
OpenOffice Application with Python
ODP
從doc、docx、odt到Google Docs
ODP
More Girls – Creating a Community of Diversity
ODP
Welcome to Apache OpenOffice 4
ODP
OpenOffice, Open Business
ODP
Multimedia Fun with OpenOffice Calc
ODP
Welcome to Apache OpenOffice 3.4 COSCUP 2012
ODP
Mosaic Fun with OpenOffice Calc
ODP
GNU Autoconf / Automake #4
ODP
GNU Autoconf / Automake #1
ODP
Solitaire with Greenfoot #2/4
ODP
Welcome to Apache OpenOffice 3.4
ODP
OpenOffice UNO Application on Android
ODP
OpenOffice.org Magic Sandbox
ODP
Mailing Lists and IRC
ODP
GNU Build System
ODP
patch和diff
ODP
OpenOffice.org UNO Magic
A Room of WikiWomen's Own
Office寶可夢GO IV計算機
OpenOffice Application with Python
從doc、docx、odt到Google Docs
More Girls – Creating a Community of Diversity
Welcome to Apache OpenOffice 4
OpenOffice, Open Business
Multimedia Fun with OpenOffice Calc
Welcome to Apache OpenOffice 3.4 COSCUP 2012
Mosaic Fun with OpenOffice Calc
GNU Autoconf / Automake #4
GNU Autoconf / Automake #1
Solitaire with Greenfoot #2/4
Welcome to Apache OpenOffice 3.4
OpenOffice UNO Application on Android
OpenOffice.org Magic Sandbox
Mailing Lists and IRC
GNU Build System
patch和diff
OpenOffice.org UNO Magic
Ad

Solitaire with Greenfoot #2