问题
I was coding a program using Java for a clock that is able to "tick" but there are problems with it. I assume it has something to do with the getter and setters, or the toString() method.
counter class
package clock;
public class Counter
{
private int _count;
private String _name;
public String getName(){return _name;}
public void setName(String _name){this._name = _name;}
public int getCount(){return _count;}
public void setCount(int _count){this._count = _count;}
public Counter(String name)
{
_name = name;
_count = 0;
}
public void Increment()
{
_count++;
}
public void Reset()
{
_count = 0;
}
}
clock class
package clock;
import java.util.Calendar;
public class Clock {
private Counter _secCounter;
private Counter _minCounter;
private Counter _hrCounter;
public Clock()
{
this._secCounter = new Counter("Seconds");
this._minCounter = new Counter("Minutes");
this._hrCounter = new Counter("Hour");
Calendar currentTime = Calendar.getInstance();
_secCounter.setCount(currentTime.get(Calendar.SECOND));
_minCounter.setCount(currentTime.get(Calendar.MINUTE));
_hrCounter.setCount(currentTime.get(Calendar.HOUR));
}
public Counter SecCounter(){return _secCounter;}
public Counter MinCounter(){return _minCounter;}
public Counter HrCounter(){return _hrCounter;}
public String Tick()
{
_secCounter.Increment();
if (_secCounter.getCount() == 60)
{
_secCounter.Reset();
_minCounter.Increment();
}
if (_minCounter.getCount() == 60)
{
_minCounter.Reset();
_hrCounter.Increment();
}
if (_hrCounter.getCount() == 24)
{
_hrCounter.Reset();
}
return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString();
}
@Override
public String toString()
{
return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString();
}
}
Main Class
package clock;
public class Main {
public static void main(String[] args)
{
Clock myClock = new Clock();
for (int i = 0; i < 10; i++)
{
String currentTime = myClock.Tick();
System.out.println(currentTime);
i = 0;
}
}
}
It outputs
clock.Counter@5c647e05:clock.Counter@33909752:clock.Counter@55f96302
Kinda new to Java and was translating the code from C#. Thanks for the help!
回答1:
As far as I can tell, you need to call _**Counter.getCount(), not toString(). You never overwrote the toString method, and so to get your clock's value back you need to use the method you wrote for getting the counter's value. If you want it to report it's name as well, you'll need to overwrite the toString() method in Counter to return something like return getName() + ": " + getCount();
回答2:
I recommend using java.util.Timer
to execute Clock.Tick() method every 1 second.
public static void main(String[] args) {
Clock myClock = new Clock();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
String currentTime = myClock.Tick();
System.out.println(currentTime);
}
}, 0, 1000);
}
You first create new Timer
instance. Call its schedule
method to make TimerTask
run every 1000 milliseconds with initial delay 0.
来源:https://stackoverflow.com/questions/44275542/clock-that-is-able-to-tick