如何在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); }
我将从不同的类调用rangeChecking方法,每次调用enterUserInput时,rangeChecking方法都会有所不同
您应该使用界面来执行此操作。
您将声明所需函数的接口传递给方法,并调用所需的方法。 将调用的具体方法是实现类实现的方法。
它被称为战略设计模式 。
代码示例:
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 }
使用该方法签名创建一个接口,并使用您的方法实现传递它的匿名子类。
// 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!"); }
在未来的java版本中(随着lambdas的出现)你不必定义接口和匿名内部类 – 将有特定的lambda语法,它将编译为等效的字节码。
[1] 不能引用在不同方法中定义的内部类中的非final变量
不是我所知道的。 这通常通过创建具有所需方法的接口并传递实现该接口的类来完成。
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)) ... }
你可以使用javareflection。
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); }
有些方法可以使用Reflection
实现这一点,但最好避免使用。 你想要做的是创建一个界面,如:
public interface RangeChecker { boolean rangeChecking(double input); }
然后为每个范围检查方法创建实现:
public class SomeRangeCheck implements RangeChecker { public boolean rangeChecking(double input) { // do something } }
然后你的另一个方法的签名变成:
public void enterUserInput(JTextField inputField, JTextField outputField, RangeChecker rangeChecking)
您可以传递任何实现RangeChecker接口的对象。
您可以创建定义所需方法的接口,并传递实现该接口的类的对象。 通常,匿名类用于此。
interface Worker { public void callMe(); } public function callWorker(Worker w) { w.callMe(); } callWorker(new Worker() { public void callMe() { System.out.println("Hi there!"); });
你不能在java中传递方法。
你所做的是声明一个接口,并将一个实现接口的类作为参数,这样你就可以通过参数调用该函数。
你可以在你的代码中创建一个匿名类。
不,传递方法作为参数是不可能的。
我想,最好的方法是创建一个接口,编写一个实现它的类(根据需要修改实现)并将类的对象作为参数传递。
您也可以使用reflection,将methodName作为String传递给您的方法。 并使用reflection来调用该方法。
希望这可以帮助 :-)