重写一个方法,让所有父级都在该方法中工作?

发布于 2024-11-25 18:30:38 字数 1145 浏览 4 评论 0原文

我有一个 B 类,它从 A 类扩展,B 类覆盖 A 类的方法:

public class ClassB extends ClassA {

    @Overrides
    protected void DoSomething() {
        super.DoSomething();
        // Some ClassB code here, called WorkB
    }
}

之后,我创建一个 B 类对象,除了 DoSomething() 中 A 的版本之外,我还需要做一些额外的事情:

ClassB objB = new ClassB() {

    @Overrides
    protected void DoSomething() {
        // I need to do something more here
    }

}

我需要知道我是否可以完成所有 ClassA 工作、ClassB 工作,然后在此方法中为 objB 添加一些其他代码?我有一个解决方案:在ClassB中创建一个public方法,它执行WorkB,然后在objB中,只需调用的方法。但我想知道是否有另一种方法(无论我的解决方案更糟还是更好!)。

编辑:我将总结这个问题,以便您可以轻松理解它:

  • Class AdoSomething 方法执行名为 WorksA 的操作。
  • Class B 覆盖 doSomething,调用 super.doSomething() 和一些额外的代码,这意味着它执行 WorksA 和一个名为 WorksB 的额外代码。
  • 对象objB doSomething() 方法必须执行 WorksAWorksB 以及另一个名为 WorksBExtra 的额外操作。

就这样。

I have a class B, that extends from class A, class B overrides a class A's method:

public class ClassB extends ClassA {

    @Overrides
    protected void DoSomething() {
        super.DoSomething();
        // Some ClassB code here, called WorkB
    }
}

After that, I create a class B object, and I need do something extra in addition to what A's version in DoSomething():

ClassB objB = new ClassB() {

    @Overrides
    protected void DoSomething() {
        // I need to do something more here
    }

}

I need to know if I can do ALL of ClassA works, ClassB works, then add some other code for objB in this method? I have a solution: create a public method in ClassB, that do WorkB, then in objB, just call the method. But I want to know if there is/are another way (no matter worse or better my solution!).

EDIT: I will summarise the question so you can easily understand it:

  • Class A's doSomething method does something called WorksA.
  • Class B overrides doSomething, call super.doSomething() and some extra code, which mean it does WorksA and an extra called WorksB.
  • Object objB doSomething() method has to do WorksA, WorksB and another extra called WorksBExtra.

That's all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-12-02 18:30:38

是的,只要先调用 super.doSomething() ,它就会抛出 A 类,然后是 B 类。之后你就可以做特定的事情了。

public class A {
public void doSomething() {
    System.out.println("A, ");
}

}

public class B extends A {
public void doSomething() {
    super.doSomething();
    System.out.println("B, ");
}

}

public static void main(String[] args) {
    B b = new B() {
        @Override
        public void doSomething() {
            super.doSomething();
            System.out.println("new B");
        }
    };
    b.doSomething();
}

输出 A、B、新 B

Yes just call super.doSomething() first and it will throw up until Class A and then Class B. After that you can do specific stuff.

public class A {
public void doSomething() {
    System.out.println("A, ");
}

}

public class B extends A {
public void doSomething() {
    super.doSomething();
    System.out.println("B, ");
}

}

public static void main(String[] args) {
    B b = new B() {
        @Override
        public void doSomething() {
            super.doSomething();
            System.out.println("new B");
        }
    };
    b.doSomething();
}

Outputs A, B, new B

眼前雾蒙蒙 2024-12-02 18:30:38

您的对象是 ClassB 匿名子类的实例,您可以以完全相同的方式进行重写:

ClassB objB = new ClassB() {

    @Override
    protected void DoSomething() {
        super.DoSomething();
        // do something more here just for this object.
    }

}

此代码将首先调用 DoSomething()ClassB 版本,其中调用ClassA版本。因此,最终效果是首先执行 ClassA 内容,然后执行 ClassB 内容,最后执行特定于匿名子类的内容。

我认为你的注释有误。它是没有“s”的@Override。可能只是一个错字。

另请注意,您的 DoSomething() 是相当非标准的命名。您最好按照 Java 中通常命名方法的方式将其称为 doSomething()

Your object is an instance of an anonymous subclass of ClassB, and you can override in exactly the same way:

ClassB objB = new ClassB() {

    @Override
    protected void DoSomething() {
        super.DoSomething();
        // do something more here just for this object.
    }

}

This code will first call the ClassB version of DoSomething(), which calls the ClassA version. So the net effect is to first do the ClassA stuff, then the ClassB stuff, then the stuff specific to the anonymous subclass.

I think you had the annotation wrong. It's @Override with no "s". Probably just a typo.

Also note that your DoSomething() is rather non-standard naming. You'd be better off calling it doSomething() in accordance with the way methods are usually named in Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文