简体   繁体   English

无法理解此输出

[英]Unable to understand the output of this

I have this code... 我有这个代码...

public class BackHanded {
int state = 0;

BackHanded(int s) {
    state = s;
}

public static void main(String... hi) {
    BackHanded b1 = new BackHanded(1);
    BackHanded b2 = new BackHanded(2);
    System.out.println(b1.go(b1) + " " + b2.go(b2));
}

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
    }
    return ++this.state;
   }
}

Why the statement return ++this.state; 为什么该语句return ++this.state; executed twice here in the second method call? 在第二个方法调用中在这里执行两次?

EDIT: I expected the output to be 2 6. But I got 2 7. 编辑:我期望输出为26。但我得到2 7。

The method calls itself within itself using recursion. 该方法使用递归调用自身。 The recursion does not occur on the first invocation since state != 2 , however the second invocation satisfies the conditional this.state == 2 causing the method to recurse. 由于state != 2 ,因此第一次调用不会发生递归,但是第二次调用满足条件this.state == 2从而导致方法递归。

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
    }
    return ++this.state;
   }
}

The execution of the second invocation of this method b2.go(b2) occurs in this manner: 以这种方式执行此方法b2.go(b2)的第二次调用:

  1. The conditional is evaluated and since state ==2 we fall into the conditional block. 对条件进行评估,由于state ==2我们进入了条件块。
  2. Within the conditional block state is assigned to 5. 在条件块内,状态分配为5。
  3. The method then invokes itself (recursion). 然后,该方法将调用自身(递归)。
  4. We start at the conditional once again, this time state != 2 so we skip the conditional. 我们再次从条件开始,这次是state != 2因此我们跳过了条件。
  5. The method returns ++this.state or 6 and finishes the invocation of itself. 该方法返回++this.state或6并完成对自身的调用。
  6. Execution returns to the first invocation, which just finished executing the conditional block, so we head to the return statement ++this.state or 7. 执行返回到第一次调用,该调用刚刚完成了条件块的执行,因此我们返回返回语句++this.state或7。

Its because of the recursion. 这是因为递归。 First the return statement is called directly from System.out.println(b1.go(b1) + " " + b2.go(b2)); 首先,直接从System.out.println(b1.go(b1) + " " + b2.go(b2));调用return语句System.out.println(b1.go(b1) + " " + b2.go(b2)); and then this method itself calls a go() method but this time if (this.state == 2) is not fulfilled so recursion breaks at this second call. 然后此方法本身调用go()方法,但是这次if (this.state == 2)未实现,则递归在第二次调用时中断。

And this way you can obtain such a callstack: 这样,您可以获得这样的调用栈:

call go(b) //b.state == 2
  call go(b) //b.state == 5
  return ++this.state;
return ++this.state;

As it was stated in Arnaud Denoyelle's answer. 正如Arnaud Denoyelle的回答所述。

Since the second BackHanded instance b2 has a value of 2, once it enters the method go it checks if the state is 2 and since it is, it executes another instance of go , now since state is no longer 2 it executes return ++this.state; 由于第二个BackHanded实例b2的值为2,一旦进入方法go它将检查state是否为2,并且由于state为2,它将执行go另一个实例,因为state不再为2,它将执行return ++this.state; after it finishes, it returns to the first call of go (since it didnt finish it yet and executes return ++this.state; and completes the first go method call. 完成后,它返回到go的第一个调用(因为它尚未完成并执行return ++this.state;并完成第一个go方法的调用。

If you want for it to execute only once add a break call inside the if statement. 如果只想让它执行一次,则在if语句中添加一个break调用。

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
        break;
    }
    return ++this.state;
    }
}

b1 is initialized with 1 and you do b1初始化为1然后执行

System.out.println(b1.go(b1) + " " + b2.go(b2));

So first look at b1 . 所以首先看b1

Value is 1, the if statement doesn't trigger, the ++ is executed and the output is 2 . 值为1, if语句不触发,执行++ ,输出为2

Now to b2 which is initialized with 2. 现在到用2初始化的b2

The if is triggered because the value is 2, value set to 5 and you call recursivly again. 因为值2触发了if ,将值设置为5,然后再次递归调用。 Now the value is 5, the if doesn't trigger and the value is increased to 6. The function returns to after the recursive call, at which point the value was already 6 and is again increased to 7. Output is 7 . 现在该值为5, if不触发,则该值增加到6。该函数在递归调用之后返回,此时该值已经是6,然后又增加到7。输出是7

when the go(b2) is called
  checks(b2.state==2) is true then
    b2.state changes to 5 then
    on coming to return ++(b2) called
  this call will return value 6 now the return statement will be as ++(6) 
    on incrementing 6 it returns 7

Because in seconde case value of state is 2, So if (this.state == 2) becomes true then state = 5 and next line is go that means same method called again. 因为在第二种情况下state的值为2,所以if (this.state == 2)变为true,则state = 5且下一行go ,意味着再次调用了相同的方法。

In this call state value is 5 and return ++this.state means state = 6, and return to the previous call 在此调用中,状态值为5并返回++this.state表示状态= 6,并返回到上一个调用

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this); //returned here
    }
    return ++this.state;
   }
}

and last return will be ++this.state means ++6 that means state = 7. 最后一个返回值将是++this.state表示++ 6表示state = 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM