Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用多个参数创建稳定的环绕变量函数?_Java_Java 8_Runnable - Fatal编程技术网

Java 如何使用多个参数创建稳定的环绕变量函数?

Java 如何使用多个参数创建稳定的环绕变量函数?,java,java-8,runnable,Java,Java 8,Runnable,一个类有许多具有相似结构的方法 public void notifyProcessingAboutReplay(String transactionId, int matchId, boolean replay){ // PART 1 //------------ // much preparation and checking with possible returns, // that all results in a correct item inst

一个类有许多具有相似结构的方法

public void notifyProcessingAboutReplay(String transactionId, int matchId, boolean replay){
    //    PART 1
    //------------
    // much preparation and checking with possible returns,
    // that all results in a correct item instance. 
    //-----------

    //     PART 2
    // here can be one or many calls, connected to that item
    // and maybe to parameters after the second one
    item.setReplay(replay);
    item.rewind();
}
每个方法都有两个或多个参数。方法的第一部分对前两个参数进行检查,并可能创建一个项以供进一步工作。也许它只是回来了。第一部分在所有方法中都是相同的

方法的第二部分差别很大。它们在第二个项目和参数(如果有)之后处理该项目和参数


我想在一个基本方法和许多分部方法中分离这两个部分,这些方法将被传递到基本方法中,可能作为
可运行的
。但是我不能将variant
Runnable
称为
item.variantRunnable()
,更不用说
item.variantRunnable(arg1,arg2…)
。显然,我对注入和
可运行性的了解太少。

您可以创建一个参数类:

class Parameter {
  Item item;
  boolean replay;
  //other params
}
并将其用作分部方法的单个入口点:

void baseMethod(Consumer<Parameter> partialMethod, Parameter param) {
  //common stuff
  param.setItem(item);
  //set other relevant things
  partialMethod.accept(param);
}

//example use:
baseMethod(this::partialMethod1, new Parameter(replay=true, ...));

void partialMethod1(Parameter param) {
  //do what you gotta do
  param.getItem().setReplay(param.getReplay));
  param.getItem().rewind();
}
void-baseMethod(消费者部分方法,参数参数){
//普通的东西
参数设置项(项);
//设置其他相关事项
partialMethod.accept(参数);
}
//示例用法:
baseMethod(this::partialMethod1,新参数(replay=true,…);
void partialMethod1(参数param){
//做你该做的
param.getItem().setReplay(param.getReplay));
参数getItem().rewind();
}

AOP-before-advice?@sidgate“我不能将variant-Runnable调用为item.variantRunnable()”。我找到的任何答案都不能这样使用。之前的工作耗时数小时,结果是负面的。文章必须简短易懂,并包含所有必要的信息。我看不出有什么办法可以改进它。根据你的空话,你也是。谢谢。看来,这是可行的。如果方便的话,我会试着看看。但我不确定我今天能应付得来。