您好,感謝您的幫助,您可以提供,需要幫助搞清楚爲什麼我在我的TestClock.java程序
得到一個java.lang.StackOverflowError的我還是很新的Java和我需要一些幫助搞清楚爲什麼我的程序不起作用。當我編譯時,一切都看起來不錯,我使用了兩個命令行參數(11:45:12 11:48:13)。當我運行該程序時,它會反彈出這個錯誤:
Exception in thread "main" java.lang.StackOverflowError at Clock.toString(Clock.java:37)
我忘了怎麼做?任何想法我需要修復?
下面是代碼:
對於我的時鐘類:
//header files
import java.time.LocalTime;
import static java.lang.System.out;
// creating class clock
public class Clock {
// private data fields
private LocalTime startTime;
private LocalTime stopTime;
// no argument cosntructor to initilize startTime to current time
protected Clock() {
startTime = LocalTime.now();
}
//method start() resets the startTime to the given time
protected LocalTime start() {
startTime = LocalTime.now();
return startTime;
}
//method stop() sets the endTime to given time
protected LocalTime stop() {
stopTime = LocalTime.now();
return stopTime;
}
//getElapsedTime() method returns elapsed time in sconds
private void geElapsedTime() {
long elapsedTime = stopTime.getSecond() - startTime.getSecond();
out.println("Elapsed time is seconds: " + elapsedTime);
}
public String toString() {
return toString();
}
}
對於我TestClock類:
// header files
import java.time.LocalTime;
import static java.lang.System.err;
import static java.lang.System.out;
// creating class of TestClock
class TestClock {
// construct a clock instance and return elapsed time
public static void main(String[] args) {
// creating object
Clock newClock = new Clock();
// checking the condition using loop
if (args.length == 2) {
LocalTime startTime = LocalTime.parse(args[0]);
LocalTime endTime = LocalTime.parse(args[1]);
}
else {
err.println("Application requires 2 command line arguments");
System.exit(1);
}
// display new clock value
out.println(newClock);
}
}
解釋此方法的目的'public String toString(){ return toString();在時鐘類中 –