java 虚函数

猜猜这里的代码输出的结果是多少?

 

package test;
public class ConstructorExample {
    static class Foo {
        int i;

        Foo() {
            i = 1;
            int x = getValue();
            System.out.println(x);
        }

        protected int getValue() {
            return i;
        }
    }

    static class Bar extends Foo {
        int j;

        Bar() {
            j = 2;
        }

        @Override
        protected int getValue() {
            return j;
        }
    }

    public static void main(String[] args) {
        Bar bar = new Bar();
    }
}

 

java中的虚函数表的引用,竟让在对象的构造函数执行之前就已经被关联到对象了??跪了

posted on 2015-08-15 13:23  寸草之心  阅读(1530)  评论(0编辑  收藏  举报

导航