正确覆盖 Grandparent 方法,已在 Parent 方法中实现

Correctly override Grandparent method, already implemented in Parent method

编辑:看来我的问题完全来自其他地方。如果您还没有阅读本主题的其余部分,我有一个 class A、一个扩展 class Aclass B 和一个扩展 class Bclass BDecorator。 我以为我无法在我的装饰器中覆盖 class B 中的特定函数,但经过几天的测试和试验,我的问题似乎出在我创建线程的方式上。 class A 实现了 Runnable,并且有一个函数 listen() 创建线程并调用 start()。 我使用参数 this 创建线程。这很重要,因为在创建线程的特定时间,this 将是 class B.

类型
new BDecorator(new B());
//             ^ The thread is created with the creation of this object, before it can be passed as an argument to BDecorator

由于线程属于 class B,它将调用用于创建它的 B objectfoo()

虽然我不会说这个问题已经解决,但它现在是一个完全不同的问题,与多态性无关。谢谢 @akuzminykh、@dreamcrash 和@Itay Dumay 帮助我尝试解决这个问题,并正确地格式化了这个 post.


我有一个实现 Runnable 的抽象 class AArun() 方法(缩写)中执行以下操作:

final public void run() {
    try {
        this.foo(message);
    }
    //...
}

Class A还有以下功能:

protected abstract void foo(String message);

装饰器的第一次迭代:覆盖 foo 函数。

然后我创建了一个 class B,它不是抽象的,这样做:

public class B extends A {
    // Constructors and getters/setters go here
    
    protected void foo(String message) {
        System.out.println(message);
    }
}

这段代码工作正常。从那以后,我决定通过创建装饰器向 B 添加不会改变整个应用程序工作方式的功能。

public class BDecorator extends B {
    B decoratedB;
    
    public BDecorator(B b) {
        this.decoratedB = b;
    }
    
    @Override
    protected void foo(String message) {
        this.decoratedB.foo("Testing a foo decorator: " + message);
    }
}

简而言之,我正在尝试重写父项中的方法,该方法已经从他们自己的父项中实现了所述方法(父项中的孙子覆盖方法,祖父项中的实现方法)。

装饰器的第二次迭代:覆盖在 B.

中创建的新栏函数

奇怪的是我还有一个用B写的方法,像这样:

public class B extends A {
    // Constructors and getters/setters go here

    protected void foo(String message) {
        System.out.println(message);
    }

    protected void bar(String message) {
      System.out.println(message);
    }
}

barclass A 中不存在。 我可以确认该程序将实际调用 BDecorator::bar() 并使用以下覆盖(当我放置断点时):

public class BDecorator extends B {
    B decoratedB;
    
    public BDecorator(B b) {
        this.decoratedB = b;
    }
    
    @Override
    protected void foo(String message) {
        this.decoratedB.foo("Testing a foo decorator: " + message);
    }

    // This method wasn't in A. It was only present in B, and for some reasons it gets called.
    @Override
    protected void bar(String message) {
        this.decoratedB.bar("Testing a bar decorator: " + message);
    }
}

这是我的主程序中发生的事情:

B firstTestSubject = new BDecorator(new B(...));
BDecorator secondTestSubject = new BDecorator(new B(...));

// Let's test both foo functions
firstTestSubject.foo("Hello world!");
secondTestSubject.foo("Hello world!");

// Now let's test both bar functions
firstTestSubject.bar("Goodbye world!");
secondTestSubject.bar("Goodbye world!)";

输出:

> Hello world!
> Hello world!
> Testing a bar decorator: Goodbye world!
> Testing a bar decorator: Goodbye world!

问题是:为什么 BDecoratorfoo override 从未被调用过?

The question is: Why is the foo override from BDecorator never called?

它被称为:

BDecorator class 添加以下 Statement:

@Override
protected void foo(String message) {
    System.out.println("Do I even exist?");
    this.decoratedB.foo("Testing a foo decorator: " + message);
}

输出:

Do I even exist?
Testing a foo decorator: Hello world!
Do I even exist?
Testing a foo decorator: Hello world!
Testing a bar decorator: Goodbye world!
Testing a bar decorator: Goodbye world!

调用BDecoratorclass的foo方法,然后将调用转发给foomethod从B传递给[=的构造函数14=] 在你的例子中:

new BDecorator(new B(...));

和来自 class B.

的实例对象