我们可以覆盖静态方法吗?为什么输出如此?

时间:2018-06-13 06:36:24

标签: java method-overriding

class Base {

    public static void display() {
        System.out.println("Static or class method from Base");
    }

    public void print() {
        System.out.println("Non-static or Instance method from Base");
    }
}

class Derived extends Base {

    public static void display() {
        System.out.println("Static or class method from Derived");
    }

    public void print() {
        System.out.println("Non-static or Instance method from Derived");
    }
}

// Driver class   
public class Test {
    public static void main(String args[]) {
        Base obj1 = new Derived();
        obj1.display();
        obj1.print();
    }
}

此代码的输出显示 -

Static or class method from Base
Non-static or Instance method from Derived

任何人都可以解释我为什么会这样?

0 个答案:

没有答案