當我試圖在我的主要方法中調用另一個類的方法時,我遇到了該標題中描述的錯誤。該錯誤指向我的類TestCalculator的第三行。下面是代碼:「線程異常」main「java.lang.StackOverflowError」錯誤?
TestCalculator類
public class TestCalculator {
Double x;
TestCalculator c = new TestCalculator();
String string = "b";
Double doubleObject = 1.0;
double doublePrimitive = 2;
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
x("12 [ 3");
return new Double(0);
}
public void testParsing() {
if (c.x(doubleObject) == 17) {
System.out.println("Adding Success");}
else {
System.out.println("Adding Fail");
}
if (c.x(doublePrimitive) == 60) {
System.out.println("Multiplying Success");}
else {
System.out.println("Multiplying Fail");
}
if (c.x(string) == null) {
System.out.println("Valid operator Success");}
else {
System.out.println("Valid operator Fail");
}
}
/*
* Adds the parameter x to the instance variable x and returns the answer as a Double.
*/
public Double x(Double x){
System.out.println("== Adding ==");
x("12 + 5");
return new Double(0);
}
/*
* Multiplies the parameter x by instance variable x and return the value as a Double.
*/
public Double x(double x){
System.out.println("== Multiplying ==");
x("12 x 5");
return new Double(0);
}
}
主類
public class Main {
public static void main(String[] args) {
TestCalculator call = new TestCalculator();
call.testParsing();
}
}
我也不太清楚,爲什麼這個錯誤發生。如果有人能幫助我理解這個錯誤是什麼以及它發生的原因,那麼我自己和其他可能在未來也會遇到這個問題的人將會非常感激。謝謝。
你一遍又一遍地調用函數沒有結束,你期望什麼結果? – Li357
我很困惑如何爲每個方法賦值,然後在主方法中調用該方法。由於所有方法都有相同的名稱,這使我很難理解如何區分這些方法。 – user7128699
你是什麼意思? – Li357