Java, Call overridden method implicitly

后端 未结 3 954
耶瑟儿~
耶瑟儿~ 2021-01-06 23:37

Right now in some Java code I have something like this

class A {
 void f() {

 }
 A() {
  f();
 }
}

class B extends A{
 @Override
 void f() {
  //do some st         


        
3条回答
  •  孤城傲影
    2021-01-07 00:23

    As far as I know, there's no way to do this. AspectJ might do something similar to this, but then you'd have to tag it with an annotation, I suppose, which is hardly less work than just calling super.f(). You need to signify that the superclass method is being called, because you need to have the ability to make that decision for each subclass separately - otherwise, you might have a subclass that doesn't want to delegate anything to the superclass at all - so the default is that you just put in the code.

提交回复
热议问题