如何在Java中使用方法引用符号(Class::method)将方法作为参数传递?

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

How pass methods as parameters in Java using method reference notation (Class::method)?

问题

我想在一个 Utils 类中创建一个方法,该方法接受两个参数,第一个参数是领域类(domain class),第二个参数是领域类中的一个方法(以引用的方式传递)。这个 Utils 类有一个方法,将在领域类实例的范围内创建一个类实例,并执行这个方法。

例如,领域类:

public class DomainClass {
  
  public void dummyMethod() {}
  
}

Utils 类:

public class Utils {

  public static void execute(Class<?> clazz, Runnable referenceMethod) {
     Object objInstance = clazz.getConstructor().newInstance();
     // 在 objInstance 的范围内执行 referenceMethod
  }

}

我想要的调用类似于:Utils.execute(DomainClass.class, DomainClass::dummyMethod)。然而,这个场景存在一些问题:

  1. 我该如何为 Utils 类传递这些参数(我现在遇到了一些编译问题)?
  2. 我该如何在 objInstance 的范围内调用 referenceMethod
英文:

I want to create method in a Utils class that accepts two parameters, the first is the domain class, and the second is a method from the domain class (passed as reference). This Utils' class has a method that will create an instance of the class and execute the method in the scope of the domain class instance.

For example, the domain class:

public class DomainClass {
  
  public void dummyMethod() {}
  
}

The Utils class:

public class Utils {

  public static void execute(Class&lt;?&gt; clazz, Runnable referenceMethod) {
     Object objInstance = clazz.getConstructor().newInstance();
     // execute the &#39;referenceMethod&#39; on the scope of &#39;objInstance&#39;
  }

}

The call I want is something like: Utils.execute(DomainClass.class, DomainClass::dummyMethod). However, this scenario has some problems:

  1. How can I pass this parameters for the Utilsclass (now I'm having some compilation problems)?
  2. How can I call the 'referenceMethod' with the scope of 'objInstance'?

答案1

得分: 7

DomainClass::dummyMethod是对一个实例方法的引用,你需要提供一个对象实例来运行它。这意味着它不能是一个Runnable,但可以是一个Consumer,例如。

另外,将execute方法改造为泛型会很有帮助:

public static <T> void execute(Class<T> clazz, Consumer<T> referenceMethod) {
    try {
        T objInstance = clazz.getConstructor().newInstance();
        referenceMethod.accept(objInstance);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

现在你可以像这样调用execute方法:

Utils.execute(DomainClass.class, DomainClass::dummyMethod);
英文:

DomainClass::dummyMethod is a reference to an instance method, and you need to provide an object instance to run it. This means it cannot be a Runnable, but it can be a Consumer for example.

Also, it will help to make the execute method generic:

    public static &lt;T&gt; void execute(Class&lt;T&gt; clazz, Consumer&lt;T&gt; referenceMethod) {
        try {
            T objInstance = clazz.getConstructor().newInstance();
            referenceMethod.accept(objInstance);
        } catch (Exception e) {
            e.printStackTrace();
        }
   }

Now you can call execute like this:

    Utils.execute(DomainClass.class, DomainClass::dummyMethod);

huangapple
  • 本文由 发表于 2020年8月25日 00:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63565096.html
匿名

发表评论

匿名网友

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

确定