本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Java Pass Method as Parameter
是否可以在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方法都会不同
接口-Java Pass方法作为参数
另请参阅此答案stackoverflow.com/a/22933032/1010868
您应该使用一个接口来做到这一点。
您将声明所需函数的接口传递给该方法,然后调用所需方法。将要调用的特定方法是由实现类实现的方法。
这称为策略设计模式。
代码示例:
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
}
谢谢..今天学到了一些新东西.. :)
如果我必须选择一个类似于该想法的设计模式,请选择"命令"模式。 Stategy听起来对我来说太复杂了,它通常意味着存在几种实现。
@amit为什么要声明接口为静态?
@Celeritas,因为它是一个内部类。如果不将其作为内部类(在其自己的文件中)放置,则无需将接口声明为静态。另外,由于java8-使用引入的lambda expressions ,此答案有点过时。
创建具有该方法签名的接口,并在您的方法实现中传递它的匿名子类。
// 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]无法在用其他方法定义的内部类中引用非最终变量
Java 7没有lambda表达式。也许Java 8会在将来的某个时候使用它们。
适当注明-相应地编辑答案。
据我所知。通常,通过创建具有所需方法的接口并传递实现该接口的类来完成此操作。
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))
...
}
@CrazyCasta ..那不是Java ..您应该编写implements IRangeCheck来实现接口。
不管怎么说,这个概念很突出。
@RohitJain很好,自从我已经用Java编写任何东西以来已经有一段时间了。
@jrharshath ..是的,我没有否认..没有冒犯。
我认为,更好的名称是interface RangeChecker和GreaterThanZero implements RangeChecker。
您可以使用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);
}
当所需的方法属于祖先类和/或兼容的方法具有不同的签名时,应处理这种情况。如果通过Double.class,则void foo(double d)不会与getDeclaredMethod匹配。
您应该真正使用语言功能,例如具有他??人呈现的界面的解决方案,而不是反思。反射类型不安全,并且比常规方法调用慢。
您可以通过多种方式使用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!");
});
不可以,不能将方法作为参数传递。
我猜,最好的方法是创建一个接口,编写一个实现该类的类(根据需要修改实现),然后将该类的对象作为参数传递。
您也可以使用反射,将methodName作为字符串传递给您的方法。并使用反射来调用该方法。
希望这可以帮助 :-)
您不能在Java中传递方法。
您要做的是声明一个接口,并获得一个实现该接口的类作为参数,这样您就可以通过该参数调用该函数。
您可以在代码中创建一个匿名类。