检查触发方法的对象

时间:2013-11-12 09:59:52

标签: java

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

5 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

通过检查堆栈跟踪,使用 reflection 执行此操作,请参阅Thread#getStackTrace

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

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

但是,为什么不简单地传递一个标识符,允许您检测谁调用该方法?

答案 2 :(得分: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();
}

答案 3 :(得分:0)

我将在boolean中添加ClockDisplay标记,即isHour。并将改变结构:

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

答案 4 :(得分: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);
    }