是否有一种方法可以在Java方法中传递回调函数?
我试图模拟的行为是一个。net委托被传递给一个函数。
我看到有人建议创建一个单独的对象,但这似乎有点过分,但我意识到有时过度是做事的唯一方法。
是否有一种方法可以在Java方法中传递回调函数?
我试图模拟的行为是一个。net委托被传递给一个函数。
我看到有人建议创建一个单独的对象,但这似乎有点过分,但我意识到有时过度是做事的唯一方法。
当前回答
public class HelloWorldAnonymousClasses {
//this is an interface with only one method
interface HelloWorld {
public void printSomething(String something);
}
//this is a simple function called from main()
public void sayHello() {
//this is an object with interface reference followed by the definition of the interface itself
new HelloWorld() {
public void printSomething(String something) {
System.out.println("Hello " + something);
}
}.printSomething("Abhi");
//imagine this as an object which is calling the function'printSomething()"
}
public static void main(String... args) {
HelloWorldAnonymousClasses myApp =
new HelloWorldAnonymousClasses();
myApp.sayHello();
}
}
//Output is "Hello Abhi"
基本上,如果你想让一个接口的对象 不可能,因为接口不能有对象。
选择是让某个类实现接口,然后使用该类的对象调用该函数。 但是这种方法非常冗长。
或者,编写新的HelloWorld() (*oberserve这是一个接口而不是一个类),然后接着定义接口方法本身。(*这个定义实际上是匿名类)。 然后获得对象引用,通过它可以调用方法本身。
其他回答
创建一个接口,并在回调类中创建相同的接口属性。
interface dataFetchDelegate {
void didFetchdata(String data);
}
//callback class
public class BackendManager{
public dataFetchDelegate Delegate;
public void getData() {
//Do something, Http calls/ Any other work
Delegate.didFetchdata("this is callbackdata");
}
}
现在在你想要回调的类中实现上面的Created Interface。 同时传递你的类的“this”对象/引用回调。
public class Main implements dataFetchDelegate
{
public static void main( String[] args )
{
new Main().getDatafromBackend();
}
public void getDatafromBackend() {
BackendManager inc = new BackendManager();
//Pass this object as reference.in this Scenario this is Main Object
inc.Delegate = this;
//make call
inc.getData();
}
//This method is called after task/Code Completion
public void didFetchdata(String callbackData) {
// TODO Auto-generated method stub
System.out.println(callbackData);
}
}
这在Java 8中使用lambdas非常容易。
public interface Callback {
void callback();
}
public class Main {
public static void main(String[] args) {
methodThatExpectsACallback(() -> System.out.println("I am the callback."));
}
private static void methodThatExpectsACallback(Callback callback){
System.out.println("I am the method.");
callback.callback();
}
}
它有点旧了,但不管怎样……我发现Peter Wilkinson的答案很好,除了它不适用于像int/Integer这样的原始类型。 问题是参数[i]的. getclass(),它返回实例java.lang。整数,另一方面,getMethod(methodName,parameters[])不会正确解释(Java的错误)…
我结合了丹尼尔·斯皮沃克(Daniel Spiewak)的建议(在他的回答中);成功的步骤包括:捕捉NoSuchMethodException -> getMethods() ->通过method.getName() ->查找匹配的一个,然后显式遍历参数列表并应用Daniels解决方案,例如识别类型匹配和签名匹配。
检查闭包是如何在lambdaj库中实现的。它们实际上有一个非常类似于c#委托的行为:
http://code.google.com/p/lambdaj/wiki/Closures
public class HelloWorldAnonymousClasses {
//this is an interface with only one method
interface HelloWorld {
public void printSomething(String something);
}
//this is a simple function called from main()
public void sayHello() {
//this is an object with interface reference followed by the definition of the interface itself
new HelloWorld() {
public void printSomething(String something) {
System.out.println("Hello " + something);
}
}.printSomething("Abhi");
//imagine this as an object which is calling the function'printSomething()"
}
public static void main(String... args) {
HelloWorldAnonymousClasses myApp =
new HelloWorldAnonymousClasses();
myApp.sayHello();
}
}
//Output is "Hello Abhi"
基本上,如果你想让一个接口的对象 不可能,因为接口不能有对象。
选择是让某个类实现接口,然后使用该类的对象调用该函数。 但是这种方法非常冗长。
或者,编写新的HelloWorld() (*oberserve这是一个接口而不是一个类),然后接着定义接口方法本身。(*这个定义实际上是匿名类)。 然后获得对象引用,通过它可以调用方法本身。