class A{
protected int val;
public A(int i){
this.val = i;
}
public int getValue(){
try{
val += 10;
return val;
}finally{
System.out.println(val);
}
}
}
class B extends A{
public B(int i){
super(i);
}
public int getBValue(){
return getValue();
}
}
public class Test{
public static void main(String[] args) {
B b = new B(2);
System.out.println(b.getBValue());
}
}
這裏有兩點要注意:
(1)protected權限的數據子類與父類共享
(2)try…finally語句,即使try中使用了return等終結語句,finally裏的代碼塊仍會執行。