简体   繁体   English

Java 包装子 class 方法,以便首先调用父方法,然后调用子 class 方法

[英]Java wrap a child class method such that the parent method gets called first and then calls child class method

I have a class that has a bunch of methods.我有一个 class 有很多方法。 This class is copied in several different places with slight variances in what each method does.这个 class 被复制到几个不同的地方,每种方法的作用略有不同。 What I want to do is move the common code into a class with similar method signatures, but the parameter is a base class that the object in the parameter of the other class inherits. What I want to do is move the common code into a class with similar method signatures, but the parameter is a base class that the object in the parameter of the other class inherits. When the method in the inherited class is called, I want it to redirect to the base class method, do that code, then somehow call back into the inherited class method.当调用继承的 class 中的方法时,我希望它重定向到基本的 class 方法,执行该代码,然后以某种方式回调到继承的 class 方法。 I cannot provide the exact code, but I hope this example is sufficient.我无法提供确切的代码,但我希望这个例子就足够了。

class BaseTableVersion{
   Long getId();
   void setId(Long id);
   String getVersion();
   void setVersion(String version);
   TableType getTableType();
}

class TableVersion extends BaseTableVersion{
   Long getVersionNumber();
   void setVersionNumber(Long versionNumber);
}


//this class needs to exists in some capacity. I can't make it inherit a class as it is now because
//it already inherits another class (which I can't show or change) and java doesn't support multiple. 
// inheritence. I can add members or change methods 
class TableVersionRules{
    void create(TableVersion tableVersion){
        //do stuff
    }

    void versionChanged(TableVersion tableVersion){
          //do stuff
    }
}

//this class doesnt need to exists, its just to hopefully illustrate what i want.
class BaseTableVersionRules<B extends BaseTableVersion>{
    void create(B tableVersion){
        //do stuff
        //call TableVersionRules.create(tableVersion) somehow
    }

    void versionChanged(B tableVersion){
          //do stuff
        //call TableVersionRules.versionChanged(tableVersion) somehow
    }
}

So, conceptually what I want is to somehow link TableVersionRules and BaseTableVersionRules such that when TableVersionRules.versionChanged is called, it will call BaseTableVersionRules.versionChanged first, and then do what is in TableVersionRules.versionChanged.因此,从概念上讲,我想要以某种方式链接 TableVersionRules 和 BaseTableVersionRules,这样当调用 TableVersionRules.versionChanged 时,它将首先调用 BaseTableVersionRules.versionChanged,然后执行 TableVersionRules.versionChanged 中的操作。 What design pattern could I use for this, or does this just defy polymorphism?我可以为此使用什么设计模式,或者这是否违反了多态性?

It looks like an inheritance with child class calling parent class using "super" keyword.它看起来像一个 inheritance 与子 class 调用父 class 使用“超级”关键字。 Something like that:像这样的东西:

class TableVersionRules extends BaseTableVersionRules {
  void versionChanged(...) {
    super.versionChanged(...);
    // Do some more stuff here.
  }
}

"Super" will call the implementation from parent class, and then you can still add some more code here. “超级”将从父 class 调用实现,然后您仍然可以在此处添加更多代码。

Does it make sense?是否有意义?

UPDATE: If you can't use "extends" since class already extends something else, try using a local field:更新:如果您不能使用“扩展”,因为 class 已经扩展了其他内容,请尝试使用本地字段:

class TableVersionRules extends SomeOtherStuffThatNeedsToBeThere {
  private final BaseTableVersionRules delegate;

  void versionChanged(...) {
    delegate.versionChanged(...);
    // Do some more stuff here.
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自父 class ZD52387880E1EA22817A72D37592138 的 arraylist 中的子 Class 对象的方法调用 - Method Calls from Child Class objects in arraylist of parent class Java 父类中的方法返回Java中的子类 - Method in parent class that returns child class in java 父类方法在子类方法之前被调用 - Parent Class method is getting called before child class method Java从父类获取子方法 - Java get child method from parent class 为什么在这种情况下调用父类方法而不调用子类? - Why parent class method is getting called and not child class in this case? 父包装子类方法 - Parent wrapping child class method 为什么调用将父类作为参数的方法调用,而不是将子类作为参数获取的方法? - Why the method that gets the Father class as a parameter is called and not the method that gets the child class as a parameter? Java:声明为父类,但实例化为Child,然后在Child中使用唯一方法 - Java: Declare as a parent class, but instantiate as Child, then using the unique method in Child 为什么在本示例中未调用子类方法,因为它在Java中是动态多态性中调用的? - Why child class method is not called in this example as it is called in dynamic polymorphism in Java? Java:使用接口从父类调用子类方法 - Java : call child class method from parent class using interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM