public class Main { private int uplimit; private int value; public Main(int uplimit){ this.uplimit = uplimit; } public void tick(){ value++; if(value == uplimit){ value = 0; } } public int getValue(){ return value; } public static void main(String[] args){ Main d = new Main(24); while(true){ d.tick(); System.out.println(d.getValue()); } } }
public class Clock { private Main hour = new Main(24); private Main minute = new Main(60); private void tick(){ minute.tick(); if(minute.getValue()==0){ hour.tick(); } } public void show(){ System.out.printf("%02d:%02d ", hour.getValue(), minute.getValue()); } public static void main(String[] args) { Clock c = new Clock(); while(true){ c.tick(); c.show(); } } }
运行结果:
05:55
05:56
05:57
05:58
05:59
06:00
06:01
06:02
06:03