任何人都可以解释这个程序的输出吗?为什么是第二个值7?Java递归函数技巧
无法理解递归函数调用go(this)
public class ThisIsTricky {
int state = 0;
public ThisIsTricky(int s) {
state = s;
}
public static void main(String[] args) {
ThisIsTricky obj1 = new ThisIsTricky(1);
ThisIsTricky obj2 = new ThisIsTricky(2);
System.out.println(obj1.go(obj1) + "" + obj2.go(obj2));
}
int go(ThisIsTricky thisIsTricky) {
if (this.state == 2) {
thisIsTricky.state = 5;
go(this);
}
return ++this.state;
}
}
输出: -
2 7
正确解释 – Ironluca