簡體   English   中英

編譯器錯誤“不兼容的類型”

[英]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不返回Timevoid )的實例,因此您無法將其分配給endTime

你有這條線:

Time endTime = startTime.addMinutes(minutes);

但是你已經聲明了addMinutes:

public void addMinutes(int mins)

哪個不返回任何內容,因此沒有返回值分配給endTime。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM