Java:在“this”中调用函数类而不是子类(类似于“超级”) [英] Java: Calling function in "this" class rather than a subclass (analog to "super")

查看:319
本文介绍了Java:在“this”中调用函数类而不是子类(类似于“超级”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中是否可以以这样的方式调用可覆盖的方法:它总是执行本地定义的版本而不是从子类中重写的版本?即是否有一个模拟 super 引用这个类,而不是超类?

Is it possible in Java to invoke an overridable method in such a way that it always executes the "locally defined" version rather than an overridden version from a subclass? I.e. is there an analog to super that refers to this class, rather than the super class?

让我给一个代码示例,希望能说清楚我正在尝试做什么:

Let me give a code example to hopefully make it clear what I'm trying to do:

class A {
    void foo() {
        System.out.println("Foo from A");
    }

    void bar() {
        foo();  // <-- This is the important line!
    }
}

class B extends A {
    @Override
    void foo() {
        System.out.println("Foo from B");
    }
}

如果我做新B ()。bar(),它将调用 A bar()方法>,在 B 中调用 foo()来打印来自B的Foo。

If I do new B().bar(), it will call the bar() method defined in A, which calls foo() as overridden in B to print "Foo from B".

有没有办法可以强制 bar()方法来调用 foo() A 中定义的方法,而不是 B ?就像我可以在 B 中使用 super.foo()来调用 foo() A 中定义的方法?不幸的是,使用 this.foo()仍然会调用子类的版本。甚至类似((A)this).foo() A.this.foo()不会

Is there a way that I can force the bar() method to call the foo() method as defined in A rather than B? Just like I can use super.foo() in B to call the foo() method as defined in A? Unfortunately using this.foo() still calls the version of the subclass. Even something like ((A) this).foo() or A.this.foo() doesn't work.

显然,我可以在<$ c $中定义私有或最终版本的 foo() c> A 然后调用它。但是我希望有一个解决方案,我所做的就是将上面代码示例中的重要行更改为调用foo()的另一种方式,让它打印Foo from A,最好不要像反射这样的技巧。

Clearly, I could simply define a private or final version of foo() in A and call that instead. But I am hoping for a solution, where all I do is change the "important line" in the code sample above to a different way of invoking foo() to have it print "Foo from A", preferably without some trick like reflection.

推荐答案

要么使你的方法静态(baadddddd),要么改变你的设计。

Either make your methods static (baadddddd), either change your design.

实际上,为子类提供默认行为是没有意义的,它被定义为适应相关方法。

Indeed, it makes no sense to provide the default behavior for a subclass that it is defined to adapt itself to the concerned method.

由于你的 foo()方法似乎有所不同,你可以实现这样的策略模式:

As your foo() method seems to vary, you may implement a Strategy Pattern like this:

interface BarProcess{
    void foo();
}

public class DefaultBarProcess implements BarProcess{
    void foo() {
       System.out.println("Foo from A");
    }
}

public class AnotherBarProcess implements BarProcess{
    void foo() {
       System.out.println("Foo from B");
    }
}

class A {

    private BarProcess barProcess;

    public A(Bar barProcess){
      this.barProcess = barProcess;
    }

    void bar() {
        barProcess.foo();
    }
}

class B extends A {  //deprecated! No need to exist

}

这篇关于Java:在“this”中调用函数类而不是子类(类似于“超级”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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