protected、try...finally

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裏的代碼塊仍會執行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章