自定义方法引用

huangapple go评论169阅读模式
英文:

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&lt;MethodReference&gt; compare = (getter) -&gt; {
			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(&quot;simpleMethod&quot;, 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

huangapple
  • 本文由 发表于 2020年8月20日 18:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定