开发者

Do not debug or run in jvm,can you tell me what's the output? [closed]

开发者_JAVA技巧It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
package Test;

public class A {

    public A() {
        System.out.println("Enter construct A...");
        init();
    }

    public void init() {
        System.out.println("Enter A's init...");
    }

}

package Test;

public class B extends A {

    int i = 0; 
    int j;

    public void init() {
        System.out.println("Enter B's init...");
        i = 100;
        j = 100;
    }

    public void printAll(){
        System.out.println(i);
        System.out.println(j);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        B b = new B();
        b.printAll();
    }

}


Okay, new B gets first an A get created, first instance fields, then calling B's init() which sets i and j to 100. Then the B gets created, which initializes the i to 0.

Enter construct A...
Enter B's init...
0
100


I suppose:

Enter construct A...
Enter B's init...
100
100

Because the A constructor has to be called bevore B can be constructed and as the class you're instatiating is from type B, the init of B is called (there is no super.init() call so the init method of class B hides the one from the super class)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜