Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类上列出公共方法而不创建实例_Java - Fatal编程技术网

Java 在类上列出公共方法而不创建实例

Java 在类上列出公共方法而不创建实例,java,Java,我正在使用的测试框架有一种相当奇怪的方式将测试添加到测试套件中,如下所示: public TestSuiteAll() { super("TestSuiteAll") this.addTest(new TestTypeOld("TEST_EXECUTION")); this.addTest(new TestTypeOld("TEST_COMPLETION_1")); this.addTest(new TestTypeOld("TEST_COMPLETION_2"

我正在使用的测试框架有一种相当奇怪的方式将测试添加到测试套件中,如下所示:

public TestSuiteAll() {
    super("TestSuiteAll")

    this.addTest(new TestTypeOld("TEST_EXECUTION"));
    this.addTest(new TestTypeOld("TEST_COMPLETION_1"));
    this.addTest(new TestTypeOld("TEST_COMPLETION_2"));

    this.addTest(new TestTypeNew("TEST_NEW"));
}
例如,TEST_EXECUTION是TestTypeOld类中要调用的函数的名称

我真的不喜欢这个设计,但我坚持它。如何列出TestTypeOld中的所有函数,以便添加所有函数

我看到过一些这样的例子:

TestTypeOld testTypeOld = new TestTypeOld("");
Class testTypeOldClass = testTypeOld.getClass();
Method[] methods = testTypeOldClass.getMethods();
但这似乎真的很冗长。在不需要创建TestTypeOld实例的情况下,是否还有其他方法可以执行此操作。

您也可以执行以下操作:

TestTypeOld.class.getMethods();
.class运算符检索表示给定类的类实例。

您还可以执行以下操作:

TestTypeOld.class.getMethods();

.class运算符检索表示给定类的类实例。

您应该能够执行以下操作:

Method[] methods = TestTypeOld.class.getMethods();
下面是一个使用java.util.List的工作示例:


您应该能够做到这一点:

Method[] methods = TestTypeOld.class.getMethods();
下面是一个使用java.util.List的工作示例:

就用这个怎么样

Methods[] methods = TestTypeOld.class.getMethods();
Methods[] methods = TestTypeOld.class.getMethods();
那么您就不必创建TestTypeOld的实例了。

使用

Methods[] methods = TestTypeOld.class.getMethods();
Methods[] methods = TestTypeOld.class.getMethods();
这样就不必创建TestTypeOld的实例。

使用反射Api 例如:

使用反射Api 例如: