模擬時鐘運行 一個clock類裡面有兩個display類,分別是時針分針,通過clock實現兩個時針(對象),分針(對象)的交互. 兩個對象互相不相關,通過比它們更高級的clock來實現交互 一個clock工程裡面一個clock包,裡邊兩個類,一個display類,一個clock類. package ...
模擬時鐘運行
一個clock類裡面有兩個display類,分別是時針分針,通過clock實現兩個時針(對象),分針(對象)的交互.
兩個對象互相不相關,通過比它們更高級的clock來實現交互
一個clock工程裡面一個clock包,裡邊兩個類,一個display類,一個clock類.
-
package clock; public class display { private int value = 0; private int limit = 0; public display(int limit) { this.limit=limit; } public void increase() { value++; if(value == limit) { value = 0; } } public int getValue() { return value; } public static void main(String[] args) { display d = new display(24); for(;;) { d.increase(); System.out.println(d.getValue()); } } }
package Clocks; public class Clock { private Display hour = new Display(24); private Display minute = new Display(60); private Display second = new Display(60); public void start() { while(true) { second.increase(); if (second.getValue()==0) { minute.increase(); if(minute.getValue()==0) { hour.increase(); } } System.out.printf("%02d:%02d:%02d\n",hour.getValue(),minute.getValue(),second.getValue()); } } public static void main(String[] args) { Clock clock = new Clock(); clock.start(); } }
參考:https://blog.csdn.net/weixin_41183791/article/details/82818115