Java: Calling a super method which calls an overridden method

前端 未结 13 2261
有刺的猬
有刺的猬 2020-11-27 10:29
public class SuperClass
{
    public void method1()
    {
        System.out.println(\"superclass method1\");
        this.method2();
    }

    public void method2(         


        
相关标签:
13条回答
  • 2020-11-27 11:16

    The keyword super doesn't "stick". Every method call is handled individually, so even if you got to SuperClass.method1() by calling super, that doesn't influence any other method call that you might make in the future.

    That means there is no direct way to call SuperClass.method2() from SuperClass.method1() without going though SubClass.method2() unless you're working with an actual instance of SuperClass.

    You can't even achieve the desired effect using Reflection (see the documentation of java.lang.reflect.Method.invoke(Object, Object...)).

    [EDIT] There still seems to be some confusion. Let me try a different explanation.

    When you invoke foo(), you actually invoke this.foo(). Java simply lets you omit the this. In the example in the question, the type of this is SubClass.

    So when Java executes the code in SuperClass.method1(), it eventually arrives at this.method2();

    Using super doesn't change the instance pointed to by this. So the call goes to SubClass.method2() since this is of type SubClass.

    Maybe it's easier to understand when you imagine that Java passes this as a hidden first parameter:

    public class SuperClass
    {
        public void method1(SuperClass this)
        {
            System.out.println("superclass method1");
            this.method2(this); // <--- this == mSubClass
        }
    
        public void method2(SuperClass this)
        {
            System.out.println("superclass method2");
        }
    
    }
    
    public class SubClass extends SuperClass
    {
        @Override
        public void method1(SubClass this)
        {
            System.out.println("subclass method1");
            super.method1(this);
        }
    
        @Override
        public void method2(SubClass this)
        {
            System.out.println("subclass method2");
        }
    }
    
    
    
    public class Demo 
    {
        public static void main(String[] args) 
        {
            SubClass mSubClass = new SubClass();
            mSubClass.method1(mSubClass);
        }
    }
    

    If you follow the call stack, you can see that this never changes, it's always the instance created in main().

    0 讨论(0)
提交回复
热议问题