0

我在java中调用这个方法:

private void updateDisplay()
    {
        displayString = hours.getDisplayValue() + ":" + 
                        minutes.getDisplayValue();
    }

是什么在几小时和几分钟内触发了此方法两次:

public String getDisplayValue()
{ 
    if(value < 10) {
        return "0" + value;
    }
    else {
        return "" + value;
    }
}

我的问题是如何检查getDisplayValue该方法是按分钟还是按小时触发的?例如:

public String getDisplayValue()
    {   if(this == minutes){
        if(value < 10) {
            return "0" + value;
        }
        else {
            return "" + value;
         }
       }

完整代码:

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;    // simulates the actual display

    /**
     * Constructor for ClockDisplay objects. This constructor 
     * creates a new clock set at 00:00.
     */
    public ClockDisplay()
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        updateDisplay();
    }

    /**
     * Constructor for ClockDisplay objects. This constructor
     * creates a new clock set at the time specified by the 
     * parameters.
     */
    public ClockDisplay(int hour, int minute)
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);
    }

    /**
     * This method should get called once every minute - it makes
     * the clock display go one minute forward.
     */
    public void timeTick()
    {
        minutes.increment();
        if(minutes.getValue() == 0) {  // it just rolled over!
            hours.increment();
        }
        updateDisplay();
    }

    /**
     * Set the time of the display to the specified hour and
     * minute.
     */
    public void setTime(int hour, int minute)
    {
        hours.setValue(hour);
        minutes.setValue(minute);
        updateDisplay();
    }

    /**
     * Return the current time of this display in the format HH:MM.
     */
    public String getTime()
    {
        return displayString;
    }

    /**
     * Update the internal string that represents the display.
     */
    private void updateDisplay()
    {
        displayString = hours.getDisplayValue() + ":" + 
                        minutes.getDisplayValue();
    }
}

和:

public class NumberDisplay
{
    private int limit;
    private int value;

    /**
     * Constructor for objects of class NumberDisplay.
     * Set the limit at which the display rolls over.
     */
    public NumberDisplay(int rollOverLimit)
    {
        limit = rollOverLimit;
        value = 0;
    }

    /**
     * Return the current value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Return the display value (that is, the current value as a two-digit
     * String. If the value is less than ten, it will be padded with a leading
     * zero).
     */
    public String getDisplayValue()
    { 
        if(value < 10) {
            return "0" + value;
        }
        else {
            return "" + value;
        }
    }

    /**
     * Set the value of the display to the new specified value. If the new
     * value is less than zero or over the limit, do nothing.
     */
    public void setValue(int replacementValue)
    {
        if((replacementValue >= 0) && (replacementValue < limit)) {
            value = replacementValue;
        }
    }

    /**
     * Increment the display value by one, rolling over to zero if the
     * limit is reached.
     */
    public void increment()
    {
        value = (value + 1) % limit;
    }
}
    }
4

5 回答 5

0

我将在ie中添加一个boolean标志。并且会改变结构:ClockDisplayisHour

class ClockDisplay{
    boolean isHour;
    public ClockDisplay(boolean isHour)
        {
            hours = new NumberDisplay(24);
            minutes = new NumberDisplay(60);
            updateDisplay();
            this.isHour=isHour;
        }
    ...........
    ...........
}

现在NumberDisplay我将改变方法:

public String getDisplayValue(ClockDisplay c)
    { 
        if(value < 10) {
            return "0" + value;
        }
        else {
            return "" + value;
        }
    c.
    }

现在在方法内部,getDisplayValue()您可以调用 顶部的任何方法c,并且它可以相应地打印,因为您已经进行了isHour相应的设置。

我的设计背后的原因是:抽象是否hour应该minute被封装在ClockDisplay. 所以只需将ClockDisplay引用传递给getDisplayValue().

于 2013-11-12T10:21:13.990 回答
0

在函数声明中引入布尔参数

public String getDisplayValue(Boolean isMinute)
{   
    if(isMinute)
    {
        if(value < 10) {
            return "0" + value;
        }
        else {
           return "" + value;
        } 
    }
    else{
          // not a minute, continue
    } 
}

你可以这样称呼

    displayString = hours.getDisplayValue(false) + ":" + 
                    minutes.getDisplayValue(true);
于 2013-11-12T10:02:58.747 回答
0

通过检查堆栈跟踪来使用反射,请参阅:Thread#getStackTrace

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

浏览API,看看哪些方法对您的需求有用。

但是你为什么不简单地传递一个标识符来让你检测谁调用了这个方法呢?

于 2013-11-12T10:03:58.200 回答
0

将参数传递给getDisplayValue()这样的函数

getDisplayValue(char c)

并将您的函数定义更改为:

public String getDisplayValue(char c)
    {   
      if(c == 'h'){
        if(value < 10) {
            return "0" + value;
        }
        else {
            return "" + value;
         }
      }
      else if(c=='m'){
            return value*60;
      }
    }

并更改updateDisplay()为:

private void updateDisplay()
    {
        displayString = hours.getDisplayValue(h) + ":" + 
                        minutes.getDisplayValue(m);
    }
于 2013-11-12T10:24:11.300 回答
0

可以引入2个子类

public class HourDisplay extends NumberDisplay {
    // override getDisplayValue method the way you want
}

public class MinuteDisplay extends NumberDisplay {
    // override getDisplayValue method the way you want
}

然后在 ClockDisplay 构造函数中

public ClockDisplay()
{
    hours = new HourDisplay(24);
    minutes = new MinuteDisplay(60);
    updateDisplay();
}
于 2013-11-12T10:06:56.093 回答