为什么可以从类实例中调用静态方法

时间:2019-01-29 15:15:56

标签: java static-methods

我很努力,但是我听不懂。我不明白为什么可以通过这种方式调用静态方法。但这似乎是可能的。而且我没有剩下的照片。 你能帮助我吗?非常感谢。

interface I {
    public void m(A a);

    class IImpl {
        public static void m(B b) {
            System.out.println("m_IB");
        }
    }
}

class A extends I.IImpl implements I {
    public void m(A a) {
        System.out.println("m_AA");
    }
}

class B extends A {
    public void m(A a) {
        super.m(a);
        System.out.println("m_BA");
    }

    public static void m(B b) {
        System.out.println("m_BB");
    }
}

public class Ex3 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        a.m(b); //prints m_IB but how is it possible this invocation?
        System.out.println();
        I i = new A(); 
        i.m(b); //why does it print m_AA ? I thought It should print m_IBB
        System.out.println();
        I j = new B(); 
        j.m(b); //prints m_AA and m_BA but why isn't called the static method this time?
        System.out.println();
    }
}

0 个答案:

没有答案