当我试图在我的主要方法中调用另一个类的方法时,我遇到了该标题中描述的错误。该错误指向我的类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