英文:
Custom method reference
问题
在Java中是否可以创建带有参数的方法引用?
给我来阐述一下:
我想知道是否可以编写类似以下代码的内容:
public boolean customEquals(ClassType object) {
Predicate<MethodReference> compare = (getter) -> {
return this::getter.equals(object::getter);
};
return compare.test(MethodReference);
}
这里的ClassType
是一个类,在该类中声明了customEquals
方法。MethodReference
是指向getter方法的某个引用。
主要思想是我想将方法传递给接口,接口应该对当前对象和参数对象执行这个方法。
英文:
Is it possible to make method reference with parameter in Java?
Give me to clarify:
I want to know is it possible to write some code like this
public boolean customEquals(ClassType object) {
Predicate<MethodReference> compare = (getter) -> {
return this::getter.equals(object::getter);
};
return compare.test(MethodReference);
}
Where ClassType is class, where customEquals method is declared
MethodReference is some link to the getter
The main idea is I want to pass method to the interface, and interface should execute this method for current object and for parameter object
答案1
得分: 0
关于您的观点
>主要思想是我想将方法传递给接口,接口应该对当前对象和参数对象执行这个方法
通过使用反射,可以将方法传递以进行执行。请参考以下代码部分:
接口定义如下:
package org.test;
import java.lang.reflect.Method;
public interface SimpleInterface {
public Object exec(Method method, Object param0, Object param1);
}//接口结束
上述接口定义了一个方法,该方法接受一个 Method 作为参数。
接口的实现如下:
package org.test;
import java.lang.reflect.Method;
public class SimpleClass implements SimpleInterface{
public Integer simpleMethod(Integer a, Integer b) {return a+b;}
@Override
public Object exec(Method method, Object param0, Object param1) {
Object retVal=null;
try{retVal=method.invoke(this, param0, param1);}catch(Exception e) {e.printStackTrace();}
return retVal;
}//exec 结束
}//类结束
上述接口实现了另一个方法,该方法将被传递以进行执行。
测试类如下:
package org.test;
import java.lang.reflect.Method;
public class SimpleTester {
public static void main(String[] args) throws Exception{
SimpleClass obj=new SimpleClass();
Method method=obj.getClass().getMethod("simpleMethod", Integer.class, Integer.class);
Object val=obj.exec(method, new Integer(1), new Integer(2));
System.out.println(val);
}//main 结束
}//类结束
上述测试类获取一个方法引用,并将其传递给接口以进行执行。
上述示例使用了反射来实现这一功能。
注意:上述示例适用于 JDK8。
英文:
With reference to your point
>The main idea is I want to pass method to the interface, and interface should execute >this method for current object and for parameter object
It is possible, with reflection to pass method for execution. Refer the below code section:
package org.test;
import java.lang.reflect.Method;
public interface SimpleInterface {
public Object exec(Method method, Object param0, Object param1);
}//interface closing
The interface has a method, which takes a Method as paramert
package org.test;
import java.lang.reflect.Method;
public class SimpleClass implements SimpleInterface{
public Integer simpleMethod(Integer a, Integer b) {return a+b;}
@Override
public Object exec(Method method, Object param0, Object param1) {
Object retVal=null;
try{retVal=method.invoke(this, param0, param1);}catch(Exception e) {e.printStackTrace();}
return retVal;
}//exec closing
}//class closing
The interface implementation along with another method, which will be passed for execution.
package org.test;
import java.lang.reflect.Method;
public class SimpleTester {
public static void main(String[] args) throws Exception{
SimpleClass obj=new SimpleClass();
Method method=obj.getClass().getMethod("simpleMethod", Integer.class, Integer.class);
Object val=obj.exec(method, new Integer(1), new Integer(2));
System.out.println(val);
}//main closing
}//class closing
The class that gets a method reference and passes to the interface for execution.
The above uses reflection to achieve the functionality.
NOTE: The above example is with JDK8
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论