能够 "tick" 的时钟
Clock that is able to "tick"
我正在使用 Java 为一个能够 "tick" 的时钟编写程序,但它存在问题。我认为它与 getter 和设置器或 toString() 方法有关。
计数器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;
}
}
时钟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();
}
}
主要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;
}
}
}
输出
clock.Counter@5c647e05:clock.Counter@33909752:clock.Counter@55f96302
对 Java 有点陌生,正在从 C# 翻译代码。感谢您的帮助!
据我所知,您需要调用 _**Counter.getCount(),而不是 toString()。您永远不会覆盖 toString 方法,因此要取回您的时钟值,您需要使用您为获取计数器值而编写的方法。如果你想让它也报告它的名字,你需要将 Counter 中的 toString() 方法覆盖为 return 之类的 return getName() + ": " + getCount();
我建议使用 java.util.Timer
每 1 秒执行一次 Clock.Tick() 方法。
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);
}
您首先创建新的 Timer
实例。调用它的 schedule
方法使 TimerTask
运行 每 1000 毫秒一次,初始延迟为 0.
我正在使用 Java 为一个能够 "tick" 的时钟编写程序,但它存在问题。我认为它与 getter 和设置器或 toString() 方法有关。
计数器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;
}
}
时钟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();
}
}
主要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;
}
}
}
输出
clock.Counter@5c647e05:clock.Counter@33909752:clock.Counter@55f96302
对 Java 有点陌生,正在从 C# 翻译代码。感谢您的帮助!
据我所知,您需要调用 _**Counter.getCount(),而不是 toString()。您永远不会覆盖 toString 方法,因此要取回您的时钟值,您需要使用您为获取计数器值而编写的方法。如果你想让它也报告它的名字,你需要将 Counter 中的 toString() 方法覆盖为 return 之类的 return getName() + ": " + getCount();
我建议使用 java.util.Timer
每 1 秒执行一次 Clock.Tick() 方法。
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);
}
您首先创建新的 Timer
实例。调用它的 schedule
方法使 TimerTask
运行 每 1000 毫秒一次,初始延迟为 0.