Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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_Function - Fatal编程技术网

我需要能够通过将完整的函数调用语句作为参数传递给java来执行函数调用

我需要能够通过将完整的函数调用语句作为参数传递给java来执行函数调用,java,function,Java,Function,让我们假设我在下面 class abc{ void doSomething(String abc){ //does Something } } 我需要能够创建一个可以传递完整调用的函数 作为参数 函数调用(doSomething(abc)){} 如果可能的话,请给出建议。类似于if中发生的情况 声明 您可以传递带有一个参数且无返回类型的函数 静态void函数调用(使用者函数、字符串参数){ function.accept(argument);//使用参数调用方

让我们假设我在下面

class abc{
    void doSomething(String abc){
        //does Something
    }
}
  • 我需要能够创建一个可以传递完整调用的函数 作为参数

    函数调用(doSomething(abc)){}

  • 如果可能的话,请给出建议。类似于if中发生的情况 声明


您可以传递带有一个参数且无返回类型的函数

静态void函数调用(使用者函数、字符串参数){
function.accept(argument);//使用参数调用方法。
}
...
Abc对象=新Abc();
消费者函数=对象::doSomething;//获取函数变量。
函数调用(函数“foobar”);


由于方法是实例方法,因此需要通过类的实例获取方法引用。这就是我的
对象
。此语法适用于Java 11.0.6。

您可以传递一个带有一个参数且没有返回类型的函数

静态void函数调用(使用者函数、字符串参数){
function.accept(argument);//使用参数调用方法。
}
...
Abc对象=新Abc();
消费者函数=对象::doSomething;//获取函数变量。
函数调用(函数“foobar”);


由于方法是实例方法,因此需要通过类的实例获取方法引用。这就是我的
对象
。此语法适用于Java 11.0.6。

创建一个以使用者函数接口为参数的函数

public static void functionCall(Consumer<String> consumer, String value) {
    consumer.accept(value);
}

创建一个将使用者函数接口作为参数的函数

public static void functionCall(Consumer<String> consumer, String value) {
    consumer.accept(value);
}

您可以将方法
doSomething
放在抽象类(Abc)中,然后创建一个匿名类,并调用所需的方法,即:

public class TemplateExample {

    public static void main(String args[]) {
        TemplateExample instance = new TemplateExample();
        PrintSomething pa = instance.new PrintSomething() {
            @Override
            void print() {
                System.out.println("PrintSomething pa");
            }
        };

        PrintSomething pb = instance.new PrintSomething() {
            @Override
            void print() {
                System.out.println("PrintSomething pb");
            }
        };

        pa.print();
        pb.print();

    }

    abstract class PrintSomething {
        abstract void print();
    }  
}

您可以将方法
doSomething
放在抽象类(Abc)中,然后创建一个匿名类,并调用所需的方法,即:

public class TemplateExample {

    public static void main(String args[]) {
        TemplateExample instance = new TemplateExample();
        PrintSomething pa = instance.new PrintSomething() {
            @Override
            void print() {
                System.out.println("PrintSomething pa");
            }
        };

        PrintSomething pb = instance.new PrintSomething() {
            @Override
            void print() {
                System.out.println("PrintSomething pb");
            }
        };

        pa.print();
        pb.print();

    }

    abstract class PrintSomething {
        abstract void print();
    }  
}

如果doSomething没有返回
布尔值,那么看看它已经像
一样工作了。就像
if(doSomething(abc))
不合法一样,
functionCall(doSomething(abc))
也不合法。方法是
void
。这似乎是一个XY问题,你想用这个函数参数做什么?看看它已经像
一样工作了,如果
doSomething
没有返回
布尔值。就像
if(doSomething(abc))
不合法一样,
functionCall(doSomething(abc))
也不合法。方法是
void
。这似乎是一个XY问题,您打算如何处理此函数参数?建议在需要传入此函数的类中声明一个内部接口,然后在类的构造函数中接受该接口。(如果接口中只有一个方法,则可以传入lambda,而不是实现整个接口。)建议在需要传入此函数的类中声明内部接口,然后在类的构造函数中接受该接口。(如果接口中只有一个方法,则可以传入lambda,而不是实现整个接口。)