Java:调用调用重写方法的超级方法 [英] Java: Calling a super method which calls an overridden method

查看:44
本文介绍了Java:调用调用重写方法的超级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class SuperClass
{
    public void method1()
    {
        System.out.println("superclass method1");
        this.method2();
    }

    public void method2()
    {
        System.out.println("superclass method2");
    }

}

public class SubClass extends SuperClass
{
    @Override
    public void method1()
    {
        System.out.println("subclass method1");
        super.method1();
    }

    @Override
    public void method2()
    {
        System.out.println("subclass method2");
    }
}



public class Demo 
{
    public static void main(String[] args) 
    {
        SubClass mSubClass = new SubClass();
        mSubClass.method1();
    }
}

我的预期输出:

子类方法1
超类方法1
超类方法2

subclass method1
superclass method1
superclass method2

实际输出:

子类方法1
超类方法1
子类方法2

subclass method1
superclass method1
subclass method2

我知道从技术上讲我已经覆盖了一个公共方法,但我认为因为我正在调用超级,超级中的任何调用都会留在超级中,这不会发生.关于如何实现它的任何想法?

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

推荐答案

关键字 super 不会粘住".每个方法调用都是单独处理的,因此即使您通过调用 super 获得了 SuperClass.method1(),也不会影响您可能进行的任何其他方法调用未来.

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.

这意味着没有通过 SubClass.method2()SuperClass.method1() 调用 SuperClass.method2() 的直接方法code> 除非您使用的是 SuperClass 的实际实例.

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.

您甚至无法使用反射实现预期效果(参见 java.lang.reflect.Method.invoke(Object, Object...)).

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

似乎仍然有些混乱.让我尝试不同的解释.

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

当您调用 foo() 时,实际上是在调用 this.foo().Java 只是让您省略this.在问题的例子中,this的类型是SubClass.

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.

所以当Java执行SuperClass.method1()中的代码时,最终会到达this.method2();

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

使用super 不会改变this 指向的实例.所以调用转到 SubClass.method2() 因为 thisSubClass 类型.

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

当您想象 Java 将 this 作为隐藏的第一个参数传递时,也许更容易理解:

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);
    }
}

如果你跟踪调用栈,你会看到this永远不会改变,它总是在main()中创建的实例.

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

这篇关于Java:调用调用重写方法的超级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆