任何人都可以解釋這個程序的輸出嗎?爲什麼是第二個值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