在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗?
public int method1(int returnedValue) {
// Do something
returnedValue = 1;
this.method2(); // Where will it go after this line?
return returnedValue;
}
public void method2() {
// Do something
}
public static void main(String[] args) {
sp.method2();
Stuff sp = new Stuff();
System.out.print(returned value);
}
现在你的 main 方法不调用 method1,请考虑这个 main 方法:
public static void main(String[] args) { new Main().method1();}
giả thuyếtMain
是主类,这一行会发生什么:
this.method2();
是否在 method1 返回值之前调用了 method2,因为在调用 method2 之前没有调用 return
。
如果 method1 是这样定义的:
public void method1(int returnedvalue)
{
returnedvalue=1;
return returnedvalue;
this.method2();
}
然后method1将返回returnedvalue
而不调用method2(上面的代码当然不好,因为this.method2()
无法访问)。
method2完成并返回后,method1执行下一条语句return returnedvalue
,它将返回并在chủ yếu
中继续执行。
当然,正如其他人所说,如果在 method2 中引发异常但没有被捕获,异常将从 method2 传播到 method1,如果 method1 没有捕获它,它将传播到 main,如果 main 没有捕获它,你的应用程序将崩溃:)。
Tôi là một lập trình viên xuất sắc, rất giỏi!