能够“打勾”的时钟

时间:2017-05-31 04:16:17

标签: java real-time-clock

我正在使用Java编写一个程序来编写一个能够“滴答”的时钟,但是它存在问题。我假设它与getter和setter或toString()方法有关。

计数器类

    char c = 'a',
        *p = &c,
        *sp1[] = {"this 1","this 2", "this 3"},
        sp3[][7] = {"this 1","this 2", "this 3"},
        (*sp2)[7] = sp3,  
        *p3 = *sp3; /* pointer to first element in sp3 */

时钟类

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;
}
}

主类

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();
}  
}

输出

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;
    }


}
}

有点新的Java并且正在从C#翻译代码。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

据我所知,你需要调用_ ** Counter。 getCount(),而不是toString()。你永远不会覆盖toString方法,所以为了获得你的时钟值,你需要使用你所写的方法来获得计数器的值。如果您希望它也报告它的名称,您需要覆盖Counter中的toString()方法以返回类似return getName() + ": " + getCount();的内容

答案 1 :(得分:0)

我建议每1秒使用java.util.Timer执行一次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。