一个时钟程序,minute自加并打印值到59时变成零同时hour加一,hour到23后变成零.
public class Display {
private int limit = 0;
private int value = 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 minute = new Display(59);
for (;;) {
minute.increase();
System.out.println(minute.getvalue());
}
}
}
public class Clock {
private Display minute = new Display(59);
private Display hour = new Display(24);
void start() {
while (true) {
minute.increase();
if (minute.getvalue() == 0) {
hour.increase();
}
System.out.printf("%02d:%02d\n",hour.getvalue(),minute.getvalue());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Clock a = new Clock();
a.start();
}
}
关键问题在于怎样实现minute对象与hour对象之间的交互
END