Java 从方法接受返回类型void?

Java 从方法接受返回类型void?,java,methods,types,parameters,void,Java,Methods,Types,Parameters,Void,我正在开发一个测试类,它允许用户进行任意方法调用。然后我的班级会触发他们 static class UserClass { static String method_01() { return ""; } static void method_02() {} } class MyTestUtil { void test() { // HowTo: // performTest( <Please put your method ca

我正在开发一个测试类,它允许用户进行任意方法调用。然后我的班级会触发他们

static class UserClass {
    static String method_01() { return ""; }
    static void   method_02() {}
}
class MyTestUtil {
    void test() {
        // HowTo:
        // performTest( <Please put your method calls here> );
        performTest( UserClass.method_01() );       // OK
        performTest( UserClass.method_02() );       // compile error
    }
    void performTest(Object o) {}
    // This is only a simplified version of the thing.
    // It is okay that the UserClass.method_calls() happens at the parameter.
    // This captures only the return value (if any).
}
我做了一些研究。我实现了类
java.lang.Void
。但它只接受
null
或类型
Void
(带大V),这不是
Void
(小V),也不是用户常用的方法

// adding these overloading methods doesn't help
void this_function_accepts() {}
void this_function_accepts(Void v) {}
void this_function_accepts(Void... v) {}
void this_function_accepts(Object v) {}
void this_function_accepts(Object... v) {}

谢谢你的帮助

解决这个问题最直接的方法是让
void
方法返回,您已经调用了它。在Java中,
void
类型永远不会被接受为值类型,因此您使用的样式对
void
不起作用


另一种方法是允许用户提供一个
Runnable
,然后代表他们运行,然后让他们在
Runnable

中调用
void
方法。我不知道为什么您希望这样做
void
方法不返回任何内容。你为什么要这么做?如果没有结果,
performTest
会做什么?即使对于非void方法,您的解决方案也不会起作用。您的方法的问题是,您应该测试的方法的调用将在
performTest
方法的调用之前发生。本质上,您的
performTest
将获取被测试方法的返回值-它将无法调用该方法或向其提供任何参数。在您的示例中,performTest()没有被传递一个可以在安装和拆卸后运行的方法--它是在该方法已经运行后被调用的,方法调用的结果。那么为什么要使用
performTest
方法呢?你想做的没有意义。如果您只想调用一个方法,那么只需调用该方法即可。方法的实际参数是值。void关键字表示方法没有返回值。您不能将void方法的返回值作为值传递,因为它不存在。谢谢您的回答。我可能需要研究
Runnable
,它可能会解决我的问题。
// adding these overloading methods doesn't help
void this_function_accepts() {}
void this_function_accepts(Void v) {}
void this_function_accepts(Void... v) {}
void this_function_accepts(Object v) {}
void this_function_accepts(Object... v) {}