Soumettre la recherche
Mettre en ligne
Object-Oriented Programming Design with Greenfoot 02
•
0 j'aime
•
456 vues
imacat .
Suivre
接龍遊戲—由上而下的物件導向程式設計(下) 發表於:Greenfoot教師工作坊(五) 時間:2012/6/15(五) 14:00 地點:南港高中
Lire moins
Lire la suite
Signaler
Partager
Signaler
Partager
1 sur 37
Télécharger maintenant
Télécharger pour lire hors ligne
Recommandé
Solitaire with Greenfoot #2
Solitaire with Greenfoot #2
imacat .
Solitaire with Greenfoot #2/4
Solitaire with Greenfoot #2/4
imacat .
Carol Rodgers - National Museums Liverpool
Carol Rodgers - National Museums Liverpool
Association of Danish Museums / Organisationen Danske Museer
Jesper Langkilde import eller lokal
Jesper Langkilde import eller lokal
Association of Danish Museums / Organisationen Danske Museer
شروط ومعايير الاستفادة من الدعم المباشر للنساء الأرامل
شروط ومعايير الاستفادة من الدعم المباشر للنساء الأرامل
ياسين محمد
Fases de la entrevista
Fases de la entrevista
rutgear
Spor 3, Pauline Asingh
Spor 3, Pauline Asingh
Association of Danish Museums / Organisationen Danske Museer
Workshop 7, Samuel Bausson, Mar Dixon
Workshop 7, Samuel Bausson, Mar Dixon
Association of Danish Museums / Organisationen Danske Museer
Contenu connexe
En vedette
Katrine Segel - konservering af poul gernes skulptur
Katrine Segel - konservering af poul gernes skulptur
Association of Danish Museums / Organisationen Danske Museer
Anders Hartvig - petersborg
Anders Hartvig - petersborg
Association of Danish Museums / Organisationen Danske Museer
OpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
imacat .
Real World Social CRM use cases
Real World Social CRM use cases
Atcore Systems
Atcore Connect - SugarCRM Call Logging by the Numbers
Atcore Connect - SugarCRM Call Logging by the Numbers
Atcore Systems
45 Kirsten Prangsgaard, Nyt på fyn
45 Kirsten Prangsgaard, Nyt på fyn
Association of Danish Museums / Organisationen Danske Museer
Video para ingles
Video para ingles
7755
MCH Data to Action
MCH Data to Action
Brandon Williams
Your Legacy: Impact or influence
Your Legacy: Impact or influence
Paul Johnson
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
imacat .
Democraciasinpartidos
Democraciasinpartidos
jaime taboaca castro
lennart madsen
lennart madsen
Association of Danish Museums / Organisationen Danske Museer
Ulla Mannering
Ulla Mannering
Association of Danish Museums / Organisationen Danske Museer
Göran björnberg - Why Museums?
Göran björnberg - Why Museums?
Association of Danish Museums / Organisationen Danske Museer
Object-Oriented Programming Design with Greenfoot 01
Object-Oriented Programming Design with Greenfoot 01
imacat .
Peter Vang Petersen
Peter Vang Petersen
Association of Danish Museums / Organisationen Danske Museer
Jens Aage Søndergaard - kortlægning af de landbrugshistoriske samlinger på d...
Jens Aage Søndergaard - kortlægning af de landbrugshistoriske samlinger på d...
Association of Danish Museums / Organisationen Danske Museer
Afib slides-focused-update-2012
Afib slides-focused-update-2012
Alexandria University, Egypt
Aoife Daly - bolværkerne ved metroudgravningen
Aoife Daly - bolværkerne ved metroudgravningen
Association of Danish Museums / Organisationen Danske Museer
Louise Karlskov Skyggebjerg - har du talt med dine genstande i dag
Louise Karlskov Skyggebjerg - har du talt med dine genstande i dag
Association of Danish Museums / Organisationen Danske Museer
En vedette
(20)
Katrine Segel - konservering af poul gernes skulptur
Katrine Segel - konservering af poul gernes skulptur
Anders Hartvig - petersborg
Anders Hartvig - petersborg
OpenOffice.org Magic Sandbox
OpenOffice.org Magic Sandbox
Real 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 Numbers
45 Kirsten Prangsgaard, Nyt på fyn
45 Kirsten Prangsgaard, Nyt på fyn
Video para ingles
Video para ingles
MCH Data to Action
MCH Data to Action
Your Legacy: Impact or influence
Your Legacy: Impact or influence
Mosaic Fun with OpenOffice Calc
Mosaic Fun with OpenOffice Calc
Democraciasinpartidos
Democraciasinpartidos
lennart madsen
lennart madsen
Ulla Mannering
Ulla Mannering
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 01
Peter 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...
Afib slides-focused-update-2012
Afib slides-focused-update-2012
Aoife 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 dag
Plus de imacat .
A Room of WikiWomen's Own
A Room of WikiWomen's Own
imacat .
Office寶可夢GO IV計算機
Office寶可夢GO IV計算機
imacat .
Crossing Office Applications
Crossing Office Applications
imacat .
OpenOffice Application with Python
OpenOffice Application with Python
imacat .
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs
imacat .
More Girls – Creating a Community of Diversity
More Girls – Creating a Community of Diversity
imacat .
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4
imacat .
OpenOffice, Open Business
OpenOffice, Open Business
imacat .
Multimedia 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 2012
imacat .
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
imacat .
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
imacat .
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
imacat .
Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
imacat .
Solitaire with Greenfoot #4
Solitaire with Greenfoot #4
imacat .
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4
imacat .
OpenOffice UNO Application on Android
OpenOffice UNO Application on Android
imacat .
Mailing Lists and IRC
Mailing Lists and IRC
imacat .
GNU Build System
GNU Build System
imacat .
patch和diff
patch和diff
imacat .
Plus de imacat .
(20)
A Room of WikiWomen's Own
A Room of WikiWomen's Own
Office寶可夢GO IV計算機
Office寶可夢GO IV計算機
Crossing Office Applications
Crossing Office Applications
OpenOffice Application with Python
OpenOffice Application with Python
從doc、docx、odt到Google Docs
從doc、docx、odt到Google Docs
More Girls – Creating a Community of Diversity
More Girls – Creating a Community of Diversity
Welcome to Apache OpenOffice 4
Welcome to Apache OpenOffice 4
OpenOffice, Open Business
OpenOffice, Open Business
Multimedia Fun with OpenOffice Calc
Multimedia Fun with OpenOffice Calc
Welcome to Apache OpenOffice 3.4 COSCUP 2012
Welcome to Apache OpenOffice 3.4 COSCUP 2012
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
Solitaire with Greenfoot #3
Solitaire with Greenfoot #3
Solitaire with Greenfoot #1
Solitaire with Greenfoot #1
Solitaire with Greenfoot #4
Solitaire with Greenfoot #4
Welcome to Apache OpenOffice 3.4
Welcome to Apache OpenOffice 3.4
OpenOffice UNO Application on Android
OpenOffice UNO Application on Android
Mailing Lists and IRC
Mailing Lists and IRC
GNU Build System
GNU Build System
patch和diff
patch和diff
Object-Oriented Programming Design with Greenfoot 02
1.
接龍遊戲 由上而下的物件導向程 式設計(下)
依瑪貓/楊士青 imacat@mail.imacat.idv.tw 2012/6/15
2.
「接龍遊戲—由上而下的物件導向程式設計」簡報由 依瑪貓╱楊士青 製作,
以 創用CC Attribution-ShareAlike 3.0 Unported 授權條款 釋出。
3.
自我介紹 依瑪貓╱楊士青 臺灣師範大學資訊教育研究所碩一研究生
4.
二、設計方法 (Method)
5.
類別、物件建立好了以後, 要怎麼樣才能讓程式動起來?
6.
玩接龍的時候, 撲克牌是怎麼動的呢?
7.
撲克牌是怎麼動的?
8.
還沒翻的牌疊
9.
已翻開的牌疊
10.
暫放區的牌疊
11.
歸整區的牌疊
12.
撲克牌是怎麼動的? 還沒翻的牌疊
暫放區的牌疊 翻牌 移到暫放區其他疊 翻開來的牌疊 移到歸整區 重新翻牌 歸整區的牌疊 移到暫放區 移到暫放區 移到歸整區 移到歸整區其他疊
13.
步驟一:建立方法 還沒翻的牌疊
class UnflippedPile { 翻牌 public void flipNextCard() { } }
14.
步驟一:建立方法 翻開來的牌疊
class FlippedPile { 重新翻牌 public void returnAllCards() { } 移到暫放區 public void moveToWorking() { 移到歸整區 } public void moveToResult() { } }
15.
步驟一:建立方法 暫放區的牌疊
class WorkingPile { 移到暫放區其他疊 public void moveToWorking() { } 移到歸整區 public void moveToResult() { } }
16.
步驟一:建立方法 歸整區的牌疊
class ResultPile { 移到暫放區 public void moveToWorking() { } 移到歸整區其他疊 public void moveToResult() { } }
17.
步驟一:建立方法 請先建立沒有內容的空方法。
不需要填上內容。
18.
步驟一:建立方法 物件要做什麼動作, 我們就建立什麼方法。
19.
步驟一:建立方法 每個物體的動作,都對應到我們建立的一個 method 。
物件導向程式設計,把我們理解的抽象物件行為 ,直接對應到 method ,寫成 method 。
20.
方法建好後, 我們就來開始實作方法裏的程式碼。
21.
步驟二:方法內容實作
翻牌 flipNextCard() 用 takeTopCard() 抽出最上面的撲克牌。 用 turnFaceUp() 翻正面。 用 addCard(Card card) 把牌放到已翻開的 牌堆。
22.
步驟二:方法內容實作
強制型別轉換 Table table = (Table) getWorld() getWorld() 是 UnflippedPile 繼承自 Actor 的方法,傳回值型態是 World 。
23.
步驟二:方法內容實作
強制型別轉換 getWorld() 是 UnflippedPile 繼承自 Actor 的方法,傳回值型態是 World 。 我們要拿 Table 的 flippedPile 來 addCard , flippedPile 屬於 Table ,而 不是 World 。 Java 編譯認定 World 沒有 flippedPile ,只 有 Table 才有 flippedPile 。
24.
步驟二:方法內容實作
強制型別轉換 解決方法:強制型別轉換 type casting 。 (Table) getWorld() 和 C 語言的強制型別轉換一樣。 float pi = 3.1415926; int p = (int) pi;
25.
步驟二:方法內容實作
強制型別轉換 子類別轉到父類別,因為子類別本來就屬於 父類別,不需強制型別轉換。 World world = table; card.pile = unflippedPile; Pile pile = unflippedPile;
26.
步驟二:方法內容實作
強制型別轉換 父類別轉到子類別,才需強制型別轉換。 Table table = (Table) getWorld();
27.
步驟二:方法內容實作
強制型別轉換 只有父類別可以轉到子類別,不相關的類別 間不能轉。 以下都是錯誤範例: FlippedPile pile = (FlippedPile) unflippedPile; Card card = (Card) pile; Table table = (Table) card;
28.
步驟二:方法內容實作
強制型別轉換 強制型別轉換有危險性,教科書教很多。 因為我們已經知道這個 World 就是 Table ,這 是程式設計者已知的現實,所以無所謂。
29.
步驟二:方法內容實作
強制型別轉換 因為 Actor 不知道 Table 的存在,由 Actor 繼承來的 getWorld() ,只可能回傳 World ,不可能直接回傳 Table 。 所以父類別轉為子類別的強制型別轉換,是物件 導向程式設計常見的技巧。
30.
步驟二:方法內容實作
翻牌 flipNextCard() ** * 未翻開的撲克牌疊。 */ public class UnflippedPile extends Pile { /** * 翻一張撲克牌。 */ public void flipNextCard() { Table table = (Table) getWorld(); Card card = takeTopCard(); card.turnFaceUp(); table.flippedPile.addCard(card); } }
31.
試著執行看看。
32.
步驟二:方法內容實作 重新翻牌
returnAllCards() 用 takeTopCard() 抽出最上面的撲克牌。 用 turnFaceDown() 翻背面。 用 addCard(Card card) 把牌放到未翻開的 牌堆。 重複做到所有的牌都還回去為止。
33.
步驟二:方法內容實作 重新翻牌 returnAllCards()
歡迎大家自己實作看看, 測試自己的實力!
34.
步驟二:方法內容實作
重新翻牌 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(); } } }
35.
試著執行看看。
36.
剩下的,大家可以試著自己做做看。
37.
謝謝大家。 歡迎提出問題。
Télécharger maintenant