public class DifficultTest {
public static void main(String[] args) {
System.out.println(new B().getValue());
}
static class A {
protected int value;
public A(int v) {
System.out.println("public A(int v)执行了");
setValue(v);
}
public void setValue(int value) {
System.out.println("AsetValue(int Value)执行了");
this.value = value;
}
public int getValue() {
System.out.println("AgetValue()执行了");
try {
value++;
return value; //Execute the result returned by getValue
} finally { //finally is a certain block
setValue(value);
System.out.println(value);
}
}
}
static class B extends A {
public B() { / /subclass this
super(5);
setValue(getValue() - 3);
System.out.println("public B() executed");
}
public void setValue(int value) {
super.setValue(2 * value);
System.out.println("BsetValue(int value) executed");
}
}
}
The returned result is:
public A(int v) executed v=5
* AsetValue(int Value) executed v=10
* BsetValue(int value) executed v=10
* AgetValue() executed v=11
* AsetValue(int Value) executed v=22
* BsetValue(int value) executes v=22
* 22 (getValue returns a value of 11)
* AsetValue(int Value) executes v=16
* BsetValue(int value) executes v=16
* public B( ) executed v=16
* AgetValue() executed v=17
* AsetValue(int Value) executed v=34
* BsetValue(int value) executed v=34
* 34
* 17 (getValue returns the value of 17)