如何在Java中将方法作为参数传递?

Ind*_*411 19 java

可能重复:
Java Pass方法作为参数

是否可以在Java中将方法作为参数传递?如果我不能重复代码,那么下面的方法将是最好的行动方案.

public void enterUserInput(javax.swing.JTextField inputField, javax.swing.JTextField   outputField, method rangeChecking){

    String input;
    double output;
    input = inputField.getText();
    output = Double.parseDouble(input);
    if(rangeChecking(output)){
        outputField.setText(input);
        inputField.setText(null);
    }
Run Code Online (Sandbox Code Playgroud)

我将从不同的类调用rangeChecking方法,每次调用enterUserInput时,rangeChecking方法都会有所不同

ami*_*mit 32

您应该使用接口来执行此操作.

您将声明所需函数的接口传递给方法,并调用所需的方法.将调用的具体方法是实现类实现的方法.

它被称为战略设计模式.

代码示例:

public static interface Foo { //this is the interface declaration
    public void print();
}
public static class Bar implements Foo { //Bar is an implementing class
    public void print() {
        System.out.println("Bar");
    } 

}
public static void test(Foo foo) { //the method accepts an interface
    foo.print(); //and invokes it, the implementing class' print() will be invoked.
}
public static void main(String... args) throws Exception {
    test(new Bar()); //invoke test() with the class implementing the interface
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢..今天学到了新东西.. :) (2认同)

jrh*_*ath 8

使用该方法签名创建一个接口,并使用您的方法实现传递它的匿名子类.

// the interface that contains the method/methods you want to pass
interface MethodWrapper {
    public void method(String s); }

// ...
// in your code
// assuming that "observable" is an object with a method "setCallback" that 
// accepts the callback
// note that your method implementation will have access to your local variables

public void someMethodOfYours() {
    observable.setCallback(new MethodWrapper() {
        public void method(String s) {
            System.out.println(s);
        } }
    );
    // note that your anon subclass will have access to all your contain method 
    // locals and your containing class members, with [some restrictions][1]
}

// in the "observable" class - the class that receives the "callback method"
// ...

if( updated() ) {
    callback.method("updated!"); }
Run Code Online (Sandbox Code Playgroud)

在java的未来版本中(随着lambdas的出现)你不必定义接口和匿名内部类 - 将有特定的lambda语法,它将编译为等效的字节码.

[1] 不能引用在不同方法中定义的内部类中的非final变量


Cra*_*sta 6

不是我所知道的.这通常通过创建具有所需方法的接口并传递实现该接口的类来完成.

public interface IRangeCheck
{
    boolean check(double val);
}

public class RangeChecker implements IRangeCheck
{
    public boolean check(double val)
    {
        return val > 0.0;
    }
}

...
    public void blabla(..., IRangeCheck irc)
    {
        ...
        if(irc.check(output))
           ...
    }
Run Code Online (Sandbox Code Playgroud)

  • 无论如何,这个概念代表...... (2认同)
  • 在我看来,更好的名称将是"接口RangeChecker"和"GreaterThanZero实现RangeChecker". (2认同)

pra*_*eth 5

你可以使用java反射.

public void enterUserInput(javax.swing.JTextField inputField, javax.swing.JTextField   outputField, String methodName){

String input;
double output;
input = inputField.getText();
output = Double.parseDouble(input);
Method m = this.getClass().getDeclaredMethod(methodName, Double.class);

if((Boolean)m.invoke(this, output)){
    outputField.setText(input);
    inputField.setText(null);
}
Run Code Online (Sandbox Code Playgroud)

  • 当所需方法属于祖先类和/或兼容方法具有不同的签名时,您应该处理这种情况.`void foo(double d)`如果传递`Double.class`,则不会被`getDeclaredMethod`匹配. (2认同)