java - 如何在HistoricalMoment类中调用increment方法()?

标签 java class methods increment bluej

从今天早上开始我就一直坚持这个方法,我希望有人能帮助我。

我有以下内容:

public class HistoricalMoment{
    private String eventName;
    private ClockDisplay timeOfEvent;
    public static final int MIDNIGHT_HOUR = 00;
    public static final int MINUTE_ZERO = 00;
}

如何创建一个名为 public void addMinuteToTimeOfEvent() 的方法,该方法调用 timeOfEvent 的increment() 方法来为 timeOfEvent 添加一分钟?

这就是我所拥有的:

public void addMinuteToTimeOfEvent(){
    timeOfEvent.increment();
}

但是当我点击 BlueJ 上的编译按钮时,我收到此错误消息:“找不到符号 - 方法increment()”

预先感谢您的帮助!

这是我的类(class)的完整代码:

public class HistoricalMoment{

    private String eventName;
    private ClockDisplay timeOfEvent;

    public static final int MIDNIGHT_HOUR = 00;
    public static final int MINUTE_ZERO = 00;

    public static final String EQUINOX = "March 2013 Equinox!";
    public static final String TITANIC = "Titanic hit an iceberg!";

    /**
     * Default Constructor
     */
    public HistoricalMoment(){
        eventName = "untitled event";
        timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
    }

    /**
     * @param nameOfTheEvent the name of the event; "untitled event" if the name of the event is null or ""
     */
    public HistoricalMoment(String nameOfTheEvent){
        if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
            eventName = "untitled event";
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
        else {
            eventName = nameOfTheEvent;
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
    }

    /**
     * @param nameOfTheEvent the name of the event;
     * @param ClockDisplay, the time of the event
     */
    public HistoricalMoment(String nameOfTheEvent, ClockDisplay theTime){
        if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
            eventName = "untitled event";
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
        else {
            eventName = nameOfTheEvent;
            timeOfEvent = theTime;
        }
    }

    public void addMinuteToTimeOfEvent(){
        timeOfEvent.increment();
    }

    /**
     * the print details of time and event
     */
    public void printDetails()
    {
        System.out.println("At" + getTime() + "," + eventName);
    }
}

这是我为 ClockDisplay 类所做的:

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

    public static final int FIRST_MORNING_HOUR  = 0;
    public static final int LAST_MORNING_HOUR   = 11;
    public static final int FIRST_EVENING_HOUR      = 12;
    public static final int LAST_EVENING_HOUR       = 23;
    public static final int MINUTES_PER_HOUR        = 60;
    public static final String MORNING_SUFFIX       = "a.m.";
    public static final String EVENING_SUFFIX       = "p.m.";
    public static final int MIDNIGHT_HOUR       = 0;
    public static final int HOURS_PER_DAY       = 0;
    private boolean isAM;

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

    /**
     * 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(12);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);
        setMorn();
    }

    /**
     * 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();
        }
        if (hours.getValue() == 12)
        {
            isAM = !isAM;
        }

        updateDisplay();
    }

    private void setMorn()
    {
        isAM = true;
    }

    private void setAft()
    {
        isAM = false;   
    }

    /**
     * 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()
    {
        int hour = hours.getValue();
        String daynight;
        if (isAM = true)
        {
            daynight = "AM (midnight)";
            if (hour == 0) 
            {
                hour = 12;   
            }
            else if(hour > 0 && hour < 12){
                daynight ="AM";
            }
            else
            {
                isAM = false;
                daynight = "PM (noon)";
                if (hour == 0) 
                {
                    hour = 12;   
                }
                else if(hour < 0 && hour > 12)
                    daynight ="PM";
            }
            displayString = hour + ":" + 
            minutes.getDisplayValue() + daynight;

        }
    }
}

最佳答案

timeOfEvent是你的类的一个实例 ClockDisplay 。您正在调用方法 increment() ,实际上并不属于 ClockDisplay而是NumberDisplay 。在我看来你会想打电话 timeTick()相反,它调用 increment()minutes ( NumberDisplay 的实例属于 ClockDisplay )。

试试这个。

public void addMinuteToTimeOfEvent(){
    timeOfEvent.timeTick();
}

关于java - 如何在HistoricalMoment类中调用increment方法()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33529770/

相关文章:

java - Android webview - 在后台保持 Activity 状态并在页面更新时创建通知?

java - 如何从不同的类和包访问对象?

java - 我如何在这里删除 System.out.println()

node.js - 如何在另一个类中使用类函数?

python pygame动画无需类即可工作,但一旦我将其放入类中,它就不想工作?

php - 有没有办法将数组转换为类属性?

java - 包私有(private)类中的公共(public)方法

java - 如何一次打印一行值,如何在 JSTL 中迭代列表

java - 带有 ProgressDialog 的外部 AsyncTask 类 [更新 : and returning back?]

java - 如何在Android Studio中使用Ant工具构建项目?