gpt4 book ai didi

java - 终于没有按预期工作

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:44 25 4
gpt4 key购买 nike

<分区>

我对 finally 关键字的实际工作方式感到困惑...

Before the try block runs to completion it returns to wherever the method was invoked. But, before it returns to the invoking method, the code in the finally block is still executed. So, remember that the code in the finally block willstill be executed even if there is a return statement somewhere in the try block.

当我运行代码时,我得到了 5 而不是我预期的 10

   public class Main {

static int count = 0;
Long x;
static Dog d = new Dog(5);

public static void main(String[] args) throws Exception {
System.out.println(xDog(d).getId());
}

public static Dog xDog(Dog d) {

try {
return d;
} catch (Exception e) {
} finally {
d = new Dog(10);

}
return d;

}
}

public class Dog {

private int id;

public Dog(int id) {
this.id = id;
}

public int getId() {
return id;
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com