作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从今天早上开始我就一直坚持这个方法,我希望有人能帮助我。
我有以下内容:
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/
我是一名优秀的程序员,十分优秀!