1. The output after the test. Main() function is executed is ()
public class Main {
public static void main(String [] args){
System.out.println(new B().getValue());
}
static class A{
protected int value;
public A(int v) {
setValue(v);
}
public void setValue(int value){
this.value = value;
}
public int getValue(){
try{
value++;
return value;
} catch(Exception e){
System.out.println(e.toString());
} finally {
this.setValue(value);
System.out.println(value);
}
return value;
}
}
static class B extends A{
public B() {
super(5);
setValue(getValue() - 3);
}
public void setValue(int value){
super.setValue(2 * value);
}
}
}
Main points:
The super() method calls the constructor of the parent class
In the main method, new B() will execute the nonparametric construction of B, and B's nonparametric will access the parameter of A through super(5). In the parameter of A, the setValue(v) method is executed, but because the method is overridden in B, the setValue(v) method in B is executed at this time
The code in finally will be executed after the code in try is executed but the return value has not been returned. The return value will be saved in the temporary stack
Perform steps:
Correct answer: 22, 34, 17
public class Main {
public static void main(String [] args){
System.out.println(new B().getValue());//1.new B() accesses B without parameters
//19. Execute getvalue(), and there is no such method in B, so A is executed
//27. Output the third value 17
}
static class A{
protected int value;//6.value=10
//9.value=11
//13.value=22
//18.value=16, now the execution of new B() is over
//21.value=17
//24.value=34
public A(int v) {
setValue(v);//3.v=5 because B overrides this method, setValue in B is executed here
}
public void setValue(int value){
this.value = value;//5. value=10
//12.value=22
//17.value=16
//23.value=34
}
public int getValue(){
try{
value++;//8. Auto increment value=11,return value is 11, but execute finally before returning
//20. The value of auto increment value=17.return is 17. Save it to the temporary stack and execute finally
return value;//14. The return here is 11
//26. Back here 17
} catch(Exception e){
System.out.println(e.toString());
} finally {
this.setValue(value);//10.value=11 this here refers to B object, so 'setValue' executes B object
//21.value=17 or execute setValue of B
System.out.println(value);//13.value=22 the first value 22 is output here
//25.value=34 output the second value 34
}
return value;
}
}
static class B extends A{
public B() {
super(5);//2. Access the parameter of parent A
setValue(getValue() - 3);//7. Execute getValue() first, because B has not been overridden, execute it in A
//15. Go back here again. getValue() returns 11 and the parameter value is 8
}
public void setValue(int value){
super.setValue(2 * value);//4.value=5 and setvalue of the parent class is executed. The calculated value of the parameter is 10
//11.value=11 and then execute the parent setValue. The parameter operation value is 22
//16.value=8, parameter value is 16, execute the setValue of the parent class again
//22.value=17, execute the setValue of the parent class, and the parameter is 34
}
}
}