Presentation is loading. Please wait.

Presentation is loading. Please wait.

物件導向系統實務. * 複習物件導向程式設計 * 如何與使用者互動?何謂傾聽者 (Listener) * 範例程式 : 剪刀石頭布遊戲.

Similar presentations


Presentation on theme: "物件導向系統實務. * 複習物件導向程式設計 * 如何與使用者互動?何謂傾聽者 (Listener) * 範例程式 : 剪刀石頭布遊戲."— Presentation transcript:

1 物件導向系統實務

2 * 複習物件導向程式設計 * 如何與使用者互動?何謂傾聽者 (Listener) * 範例程式 : 剪刀石頭布遊戲

3

4 * 宣告一類別 A ( 我們曾經將類別比喻為自訂資料型態 ) class A { …… } * 宣告一物件 x, 是一個 A 類別的物件 ( 我們曾經將物件看成是一 個變數 ) A x = new A();

5 * 在撰寫 Java 程式時,經常會有臨時繼承某個類別或實作某個 介面並建立實例的需求,由於這類子類別或介面實作類別只 使用一次,不需要為這些類別定義名稱,這時可以使用匿名 內部類別來解決這個需求。匿名內部類別的語法為: new 父類別 () | 介面 () { // 類別本體實作 }; * 所以, 我們可以將上一頁宣告一個 A 類別的物件 x, 寫成 x = new A() { … };

6 * Java 是單一繼承,也就是 class B1 {…} class B2 {…} class B3 extends B1 {…} 類別 B3 只能繼承 B1 或是 B2 其中一個,不可以同時繼承 B1 和 B2 * 抽象類別( abstract class ):只要類別中,有一個方法是抽 象方法(有方法名稱,但是無方法的內容),就必須宣告為 抽象類別 * 介面 (interface) :為了可以實踐多重繼承, Java 中用 interface 來處理,在 interface 中,所有的方法都只有名稱, 沒有方法的實作內容

7 * 一個抽象類別不可以宣告物件,所以,抽象類別通常是當作 父類別被別人繼承,而繼承的子類別要實作父類別中的抽象 方法。 * 可以想像一下,父類別中有一動作是吃食物 () ,在子類別中,就 要分別定義吃食物 (){……} * 而一個類別要多重繼承時,要用下列語法: class C implements interface1, interface2, interface3

8

9

10 * WindowListener is interface which force you to override all of the methods, while WindowAdapteris implementation of WindowListener and you only need to override the method(s) that you interest to deal with. WindowListenerWindowAdapter * WindowListener is interface which mean you cant instantiation the WindowListener, while WindowAdapter is concrete class that you can use new operator to instantiation. * When you use WindowAdapter, the code is more clean where your class only override the method(s) that you want. For example:

11 * WindowListener public class CloseListener implements WindowListener { // im not interest on this event, but still need to override it @Override public void windowOpened(WindowEvent e) { } // im not interest on this event, but still need to override it @Override public void windowClosing(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { System.exit(0); } // im not interest on this event, but still need to override it @Override public void windowIconified(WindowEvent e) { } // im not interest on this event, but still need to override it @Override public void windowDeiconified(WindowEvent e) { } }

12 * WindowAdapter While using adapter the code is cleaner: // at Jframe class addWindowListener(new CloseListener()); // reusable Close Listener public class CloseListener extends WindowAdapter { @Override public void windowClosed(WindowEvent e) { System.exit(0); } } Or addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { System.exit(0); } });

13

14

15 1. package com.example.ch03_01_scissorstonepaper; 2. 3. import android.app.Activity; 4. import android.os.Bundle; 5. import android.view.Menu; 6. import android.view.View; 7. import android.widget.Button; 8. import android.widget.TextView;

16 9. public class MainActivity extends Activity { 10. Button btnScissor, btnStone, btnPaper, btnAgain; 11. TextView txtComPlayer, txtPlayer,txtResult; 12. int comPlayer; 13. int player; 14. @Override 15. protected void onCreate(Bundle savedInstanceState) { 16. super.onCreate(savedInstanceState); 17. setContentView(R.layout.activity_main); 18. btnScissor = (Button)findViewById(R.id.btnScissor); 19. btnStone = (Button)findViewById(R.id.btnStone); 20. btnPaper = (Button)findViewById(R.id.btnPaper); 21. btnAgain = (Button)findViewById(R.id.btnAgain); 22. txtComPlayer = (TextView)findViewById(R.id.txtComPlayer); 23. txtPlayer = (TextView)findViewById(R.id.txtPlayer); 24. txtResult = (TextView)findViewById(R.id.txtResult); 25. btnScissor.setOnClickListener(scissorOnClickListener); 26. btnStone.setOnClickListener(stoneOnClickListener); 27. btnPaper.setOnClickListener(paperOnClickListener); 28. btnAgain.setOnClickListener(againOnClickListener); 29. }

17 30. Button.OnClickListener scissorOnClickListener = new Button.OnClickListener( ) 31. { public void onClick(View v) 32. { // 設定玩家的出拳 33. player = 0; 34. txtPlayer.setText(" 剪刀 "); 35. comPlayer = (int)(Math.random()*3); 36. switch(comPlayer) 37. { case 0: 38. txtComPlayer.setText(" 剪刀 "); 39. break; 40. case 1: 41. txtComPlayer.setText(" 石頭 "); 42. break; 43. case 2: 44. txtComPlayer.setText(" 布 "); 45. } 46. if(player == comPlayer) 47. txtResult.setText(" 平手 "); 48. else 49. if((player+1)%3 == comPlayer) 50. txtResult.setText(" 電腦勝 "); 51. else 52. txtResult.setText(" 玩家勝 "); 53. } 54. };

18 55. Button.OnClickListener stoneOnClickListener = new Button.OnClickListener() 56. { public void onClick(View v) 57. { // 設定玩家的出拳 58. player = 1; 59. txtPlayer.setText(" 石頭 "); 60. comPlayer = (int)(Math.random()*3); 61. switch(comPlayer) 62. { case 0: 63. txtComPlayer.setText(" 剪刀 "); 64. break; 65. case 1: 66. txtComPlayer.setText(" 石頭 "); 67. break; 68. case 2: 69. txtComPlayer.setText(" 布 "); 70. } 71. if(player == comPlayer) 72. txtResult.setText(" 平手 "); 73. else 74. if((player+1)%3 == comPlayer) 75. txtResult.setText(" 電腦勝 "); 76. else 77. txtResult.setText(" 玩家勝 "); 78. } 79. }

19 80. Button.OnClickListener paperOnClickListener = new Button.OnClickListener() 81. { public void onClick(View v) 82. { // 設定玩家的出拳 83. player = 2; 84. txtPlayer.setText(" 布 "); 85. comPlayer = (int)(Math.random()*3); 86. switch(comPlayer) 87. { case 0: 88. txtComPlayer.setText(" 剪刀 "); 89. break; 90. case 1: 91. txtComPlayer.setText(" 石頭 "); 92. break; 93. case 2: 94. txtComPlayer.setText(" 布 "); 95. } 96. if(player == comPlayer) 97. txtResult.setText(" 平手 "); 98. else 99. if((player+1)%3 == comPlayer) 100. txtResult.setText(" 電腦勝 "); 101. else 102. txtResult.setText(" 玩家勝 "); 103. } 104. };

20 105. Button.OnClickListener againOnClickListener = new Button.OnClickListener() 106. { public void onClick(View v) 107. { 108. txtComPlayer.setText(" 還沒有出拳 "); 109. txtPlayer.setText(" 還沒有出拳 "); 110. txtResult.setText(" 勝負未定 "); 111. } 112. }; 113. 114. @Override 115. public boolean onCreateOptionsMenu(Menu menu) { 116. 117. // Inflate the menu; this adds items to the action bar if it is present. 118. getMenuInflater().inflate(R.menu.main, menu); 119. return true; 120. } 121. }

21 * 三個按鈕的類別相同,是否可以用陣列?? Button [] btn = new Button[3]; * 有了陣列的好處: * 可以使用迴圈 * 可以將類似的 onClick() 方法整合為一個

22 // 省略套件的程式碼 1. public class MainActivity extends Activity { 2. Button btn[] = new Button[3]; 3. Button btnAgain; 4. TextView txtComPlayer, txtPlayer,txtResult; 5. int comPlayer; 6. int player; 7. @Override 8. protected void onCreate(Bundle savedInstanceState) { 9. super.onCreate(savedInstanceState); 10. setContentView(R.layout.activity_main); 11. btn[0] = (Button)findViewById(R.id.btnScissor); 12. btn[1] = (Button)findViewById(R.id.btnStone); 13. btn[2] = (Button)findViewById(R.id.btnPaper); 14. btnAgain = (Button)findViewById(R.id.btnAgain); 15. txtComPlayer = (TextView)findViewById(R.id.txtComPlayer); 16. txtPlayer = (TextView)findViewById(R.id.txtPlayer); 17. txtResult = (TextView)findViewById(R.id.txtResult); 18. btn[0].setOnClickListener(playerOnClickListener); 19. btn[1].setOnClickListener(playerOnClickListener); 20. btn[2].setOnClickListener(playerOnClickListener); 21. btnAgain.setOnClickListener(againOnClickListener); 22. } 你可以將這裡改成迴圈嗎?

23 1. Button.OnClickListener playerOnClickListener = new Button.OnClickListener() 2. { public void onClick(View v) 3. { Button b = (Button)v; 4. // 設定玩家的出拳 5. if(b.getText().toString().equals(" 剪刀 ")) 6. { player = 0; 7. txtPlayer.setText(" 剪刀 "); 8. } 9. else 10. if(b.getText().toString().equals(" 石頭 ")) 11. { player = 1; 12. txtPlayer.setText(" 石頭 "); 13. } 14. else 15. { player = 2; 16. txtPlayer.setText(" 布 "); 17. } 18.

24 19. comPlayer = (int)(Math.random()*3); 20. switch(comPlayer) 21. { case 0: 22. txtComPlayer.setText(" 剪刀 "); 23. break; 24. case 1: 25. txtComPlayer.setText(" 石頭 "); 26. break; 27. case 2: 28. txtComPlayer.setText(" 布 "); 29. } 30. if(player == comPlayer) 31. txtResult.setText(" 平手 "); 32. else 33. if((player+1)%3 == comPlayer) 34. txtResult.setText(" 電腦勝 "); 35. else 36. txtResult.setText(" 玩家勝 "); 37. } 38. }; 39.

25 5. if(b == btn[0]) 6. { player = 0; 7. txtPlayer.setText(" 剪刀 "); 8. } 9. else 10. if(b == btn[1]) 11. { player = 1; 12. txtPlayer.setText(" 石頭 "); 13. } 14. else 15. { player = 2; 16. txtPlayer.setText(" 布 "); 17. }

26 * 請為遊戲做一個新功能,可以計算電腦的贏率 * 請將畫面中的[再來一局]按鈕做以下功能 1. 將文字區內的文字還原到一開始的畫面 2. 將電腦的贏率歸零


Download ppt "物件導向系統實務. * 複習物件導向程式設計 * 如何與使用者互動?何謂傾聽者 (Listener) * 範例程式 : 剪刀石頭布遊戲."

Similar presentations


Ads by Google