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

查看:278
本文介绍了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

我从技术上知道我已经覆盖了一个公共方法,但我认为因为我在调用super,所以在内部调用超级将留在超级,这不会发生。关于如何实现这一点的任何想法?

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.

这意味着没有直接的方式来调用 SuperClass.method2() 来自 SuperClass.method1()而不会通过 SubClass.method2()除非你'重新使用 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.

您甚至无法使用Reflection实现所需的效果(请参阅 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只是让你省略这个。在问题的示例中,的类型是 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 不会更改指向的实例。所以调用转到 SubClass.method2(),因为的类型为 SubClass

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传递这个作为隐藏的第一个时,也许更容易理解参数:

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

如果你按照调用堆栈,你可以看到永远不会更改,它始终是在 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天全站免登陆