[英]Compiler error “incompatible types”
我收到编译器错误:
Exercise.java:47:错误:不兼容的类型Time endTime = startTime.addMinutes(minutes);
^
必需:找到时间:无效1错误
我试图使用的方法是这样的:
public void addMinutes(int mins) {
this.mins += mins;
if (this.mins >= 60) { // check if over
addHours(this.mins / 60);
this.mins = this.mins % 60;
}
}
我不知道为什么。
import java.util.*;
import java.io.*;
public class Exercise {
private String exercise;
private int minutes;
private Time startTime;
private Time endTime;
private int addedminutes;
public Exercise(String exercisetype, int m, Time start) {
this.exercise = exercisetype;
this.minutes = m;
this.startTime = start;
}
public String getType() {
return this.exercise;
}
public int getMinutes() {
return this.minutes;
}
public Time getStart() {
return this.startTime;
}
public Time getEnd() {
Time endTime = startTime.addMinutes(minutes);
return endTime;
}
public int addMinutes(int added) {
addedminutes = this.minutes + added;
return addedminutes;
}
public Time setStart(Time newstart) {
this.startTime = newstart;
return newstart;
}
public String toString() {
String startStandard = startTime.getStandard();
String endStandard = endTime.getStandard();
String toReturn = (this.exercise + " for " + this.minutes + " minutes," + " from " + startStandard + " to " + endStandard);
return toReturn;
}
public boolean equals(Exercise exTwo) {
return exercise == exTwo.exercise && minutes == exTwo.minutes && startTime == exTwo.startTime;
}
private static String exercisetype;
public static String getTypes() {
String types = ("Exercise types: " + Exercise.exercisetype);
return types;
}
}
addMinutes
不返回值( void
返回类型)。 相反,它会更改调用它的Time对象的状态。
或者,更改方法以在完成后返回时间,例如:
public Time addMinutes(int mins) {
this.mins += mins;
if (this.mins >= 60) { // check if over
addHours(this.mins / 60);
this.mins = this.mins % 60;
}
return this;
}
或者将您的使用情况更改为:
public Time getEnd() {
Time endTime;
startTime.addMinutes(minutes);
//This seems wrong, by the way. startTime will be modified by this call.
endTime = startTime;
return endTime;
}
或者,更简单:
public Time getEnd() {
startTime.addMinutes(minutes);
return startTime;
}
addMinutes
不返回Time
( void
)的实例,因此您无法将其分配给endTime
你有这条线:
Time endTime = startTime.addMinutes(minutes);
但是你已经声明了addMinutes:
public void addMinutes(int mins)
哪个不返回任何内容,因此没有返回值分配给endTime。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.