public class Test {
public static void main(String[] args) {
System.out.println("new B().getValue()-----"+new B().getValue());
}
static class A{
protected int value;
public A(int v){
System.out.println("before_v-----"+v);
setValue(v);
System.out.println("after_v-----"+v);
}
public void setValue(int value){
System.out.println("before_this.value-----"+value);
this.value = value;
System.out.println("after_this.value-----"+value);
}
public int getValue(){
try{
System.out.println("before_value-----"+value);
value++;
System.out.println("after_value-----"+value);
return value;
}finally {
System.out.println("finally_before_value-----"+value);
this.setValue(value);
System.out.println("finally_after_value-----"+value);
}
}
}
static class B extends A{
public B(){
super(5);
setValue(getValue() - 3);
}
public void setValue(int value){
super.setValue(2 * value);
}
}
}
输出结果:
before_v-----5
before_this.value-----10
after_this.value-----10
after_v-----5
before_value-----10
after_value-----11
finally_before_value-----11
before_this.value-----22
after_this.value-----22
finally_after_value-----22
before_this.value-----16
after_this.value-----16
before_value-----16
after_value-----17
finally_before_value-----17
before_this.value-----34
after_this.value-----34
finally_after_value-----34
new B().getValue()-----17