小编Rom*_*lov的帖子

当对象的所有字段都设置为默认值时,为什么实例常量还有值?

这是代码:

public class Main {
    public static void main(String[] args) {
        new B();
    }
}

class A {
    A() {
        System.out.println("A constructor before");
        action();
        System.out.println("A constructor after");
    }

    protected void action() {
        System.out.println("Never called");
    }
}

class B extends A {
    private final int finalField = 42;
    private int field = 99;

    B() {
        System.out.println("B constructor");
        action();
    }

    public void action() {
        System.out.println("B action, finalField=" + finalField + ", field=" + field);
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

A constructor before
B action, …
Run Code Online (Sandbox Code Playgroud)

java polymorphism

4
推荐指数
1
解决办法
104
查看次数

标签 统计

java ×1

polymorphism ×1