在java中将方法作为参数传递

在java中将方法作为参数传递,java,methods,parameters,Java,Methods,Parameters,我正在做一个小方法,我需要传递一个方法作为参数,所以我不必重复代码。 所以我必须使用这个方法,唯一改变的是我在这个方法中使用的方法。所以,如果我可以在参数中传递一个方法,它将大大简化我的代码。 这是我的密码。我可以使用java的Reflect吗 public static void testsForLinkedLists(int potencia, int repeticoesteste, int somarAoArray, String ficheiroExcel,

我正在做一个小方法,我需要传递一个方法作为参数,所以我不必重复代码。 所以我必须使用这个方法,唯一改变的是我在这个方法中使用的方法。所以,如果我可以在参数中传递一个方法,它将大大简化我的代码。 这是我的密码。我可以使用java的Reflect吗

public static void testsForLinkedLists(int potencia,
            int repeticoesteste, int somarAoArray, String ficheiroExcel,
            int validararray, Method pushMethod)
我有两个方法,我想用它作为参数

public class measuring_tests {
    public static double timeToPushLinkedStack(int intendedPushes) {
        final LinkedStackOfStrings measuringTimeToPush = new LinkedStackOfStrings();
        final String element = "measuring_test";
        int numberOfPushesDone = 0;
        double totalPushTime = 0;
        Stopwatch stopwatch = new Stopwatch();

        while (numberOfPushesDone < intendedPushes) {

            measuringTimeToPush.push(element);

            numberOfPushesDone++;

        }
        totalPushTime = stopwatch.elapsedTime();

        while (measuringTimeToPush.size > 0) {
            measuringTimeToPush.pop();
        }

        return totalPushTime;

    }

    public static double timeToPopLinkedStack(int intendedPops) {

        final LinkedStackOfStrings measuringTimeToPop = new LinkedStackOfStrings();
        final String element = "measuring_test";

        while (measuringTimeToPop.size < intendedPops) {
            measuringTimeToPop.push(element);
        }

        double totalPopTime = 0;
        Stopwatch stopwatch = new Stopwatch();

        while (measuringTimeToPop.size > 0) {
            measuringTimeToPop.pop();
        }

        totalPopTime = stopwatch.elapsedTime();

        return totalPopTime;
    }
public class measurement\u测试{
公共静态双timeToPushLinkedStack(int-intentendpush){
最终LinkedStackOfStrings measuringTimeToPush=新LinkedStackOfStrings();
最终字符串元素=“测量测试”;
int numberOfPushesDone=0;
双总推送时间=0;
秒表秒表=新秒表();
while(numberOfPushesDone0){
measuringTimeToPush.pop();
}
返回时间;
}
公共静态双timeToPopLinkedStack(int-INTENTEDPOPS){
最终LinkedStackOfStrings measuringTimeTop=新LinkedStackOfStrings();
最终字符串元素=“测量测试”;
while(measuringtimepop.size0){
measuringTimeToPop.pop();
}
totalPopTime=stopwatch.elapsedTime();
返回时间;
}

如果所有方法都具有相同的签名
double m(int)
,则可以使用类似的内容并传递方法引用:

public static void testsForLinkedLists(int potencia,
        int repeticoesteste, int somarAoArray, String ficheiroExcel,
        int validararray, IntToDoubleFunction pushMethod)
你可以这样称呼它:

testsForLinkedLists(...., measuring_tests::timeToPushLinkedStack);

如果所有方法都具有相同的签名
double m(int)
,则可以使用类似的内容并传递方法引用:

public static void testsForLinkedLists(int potencia,
        int repeticoesteste, int somarAoArray, String ficheiroExcel,
        int validararray, IntToDoubleFunction pushMethod)
你可以这样称呼它:

testsForLinkedLists(...., measuring_tests::timeToPushLinkedStack);
好的。我明白了:)试着看看本教程: 您还可以通过以下方式获取方法:Method foothod=MyObject.class.getMethod(“foo”)

好的。我理解:)尝试查看本教程:
您还可以通过以下方式获得方法:Method foommethod=MyObject.class.getMethod(“foo”)

你想传递的所有方法都有相同的签名吗?是什么?我有4或5种方法。两种方法用于链接列表上的推送和弹出,另外两种方法用于相同但可调整大小的数组。我真的不明白你想做什么。我认为你的方法是错误的,有更好的方法。你能解释一下为什么要这样做吗你需要把它作为参数吗?因为你会用参数调用你的方法n次,你可以只调用你的方法,这将是同样的效果。我有一个巨大的方法代码来在excel中编写一些tsts数据。我正在做链接列表和可调整大小的数组中的推送和弹出,所以现在我必须重复这个代码2次,用于测试链接列表和anot如果我能通过计算push和pop的方法,我可以使用一个方法,将我想通过的方法的输出作为参数写入excel。你想通过的所有方法都有相同的签名吗?是什么?我有4或5个方法。两个方法用于push和pop的链接lIST和另外两个用于相同但可调整大小的阵列我真的无法理解你在尝试做什么。我认为你的方法是错误的,有一个更好的方法。你能解释一下为什么你需要把它作为参数吗?因为你将用参数n次调用你的方法,你可以调用你的方法,这将是相同的效果我有一个在excel中编写一些tsts数据的大量方法代码。我正在做链接列表和可调整大小的数组中的push和pop,所以现在我必须重复这段代码2次,在链接列表上进行测试,在可调整大小的测试中进行另一次。如果我能通过计算push和pop的方法,我可以使用一种方法来编写excel数据我想作为参数传递的方法的放置我已经完成了您的建议,并编辑成
公共静态无效测试链接列表(int-Potentia、int-RepeticoeStee、int-Somaroauray、String-FicheroExcel、int-validararray、method-pushMethod、IntToDoubleFunction-popMethod)
但是当我在这个测试中使用pushMethod时,链接列表会给我错误。我是这样做的:
double timetopush=pushMethod(resultadoptencia);
它说我必须创建一个pushMethod方法:/IntToDoubleFunction是一个接口-你需要调用它的方法:
double timetopush=pushMethod.applyAsDouble(resultadoptencia);
。我已经完成了您的建议,并编辑了
公开静态无效测试的链接列表(int-potenia、int-repeticoeste、int-somaroarray、String-ficheiroExcel、int-validarray、Method-pushMethod、IntToDoubleFunction-popMethod)
但是当我在这个测试中使用pushMethod时,链接列表会给我错误。我是这样做的:
double timetopush=pushMethod(resultadoptencia);
它说我必须创建一个pushMethod方法:/IntToDoubleFunction是一个接口-你需要调用它的方法:
double timetopush=pushMethod.applyAsDouble(resultadoptencia);