實現:
結構:
對象:時鐘 - 對象:小時
- 對象:分鐘
package clock:
1 package clock; 2 3 public class Display { 4 int value = 0; 5 int limit = 0; 6 7 Display(int limit) { 8 this.limit = limit; 9 } 10 11 void increase() { 12 ++ value; 13 if(value == limit){ 14 value = 0; 15 } 16 } 17 18 int getvalue() { 19 return value; 20 } 21 22 public static void main(String[] args) { //test 23 Display min = new Display(60); 24 for(;;) { 25 min.increase(); 26 System.out.println(min.getvalue()); 27 } 28 } 29 30 }Display.java
1 package clock; 2 3 public class Clock { 4 Display min = new Display(60); 5 Display hour = new Display(24); 6 7 void start() { 8 min.increase(); 9 if(min.getvalue()==0) { 10 hour.increase(); 11 } 12 } 13 14 public static void main(String[] args) { 15 Clock clock = new Clock(); 16 for(;;) { 17 clock.start(); 18 System.out.printf("%02d:%02d\n",clock.hour.getvalue(),clock.min.getvalue()); 19 } 20 } 21 22 }