extendsExample

 avatar
unknown
java
3 years ago
1.0 kB
15
Indexable
public class teacherExample {
    public static void main(String[] args){
        new B(33);
        // line 27
    }
}
class A{
    int n = 55;
    A(){
        System.out.println(this.n); //this.n = 55 -> 1th輸出
        this.A(); //Line 14
        System.out.println(this.n); 
    }
    void A(){
        System.out.println(this.n); //this.n = 55 -> 2th輸出
        this.A(this.n+1); 
        System.out.println(this.n); 
    }
    void A(int n){
        System.out.println(this.n); 
        this.n = n; 
        System.out.println(this.n); 
    }
}
class B extends A{
    int n = 66;
    B(int n){ 
        /*
        由於預設java繼承時會在建構子第一行加入
        無參數的super() 所以會執行 A() 的建構子 Line 9
        */
        System.out.println(this.n); //this.n = 66
        this.A(); //line 
        System.out.println(this.n);
    }
    void A(int n){
        System.out.println(this.n); 
        this.n = n; //
        System.out.println(this.n);
    }
}
Editor is loading...