一个简单的程序,编译可以通过但是,不出现结果,错误提示为无数个at Test. <init> (TestNonStatic.java:8), class Test {
Test() {}
Test t= new Test();
int fact(int n) {
if(n> 0) {
return t.fact(n-1)*n;//递归调用类Test内的fact函数
}
else {
return 1;
}
}
}
public class TestNonStatic {
public static void main(String[] args) {
Test t1=new Test();
int m=t1.fact(5);
System.out.println(m);
}
}
Test() {}
Test t= new Test();
int fact(int n) {
if(n> 0) {
return t.fact(n-1)*n;//递归调用类Test内的fact函数
}
else {
return 1;
}
}
}
public class TestNonStatic {
public static void main(String[] args) {
Test t1=new Test();
int m=t1.fact(5);
System.out.println(m);
}
}
导致new Test()时无穷递归最后栈溢出了
Test() {}
Test t= new Test();int fact(int n) {
if(n> 0) {
return
t.fact(n-1)*n;//递归调用类Test内的fact函数}
else {
return 1;
}
}
}
public class TestNonStatic {
public static void main(String[] args) {
Test t1=new Test();
int m=t1.fact(5);
System.out.println(m);
}
}