Java反射:如何获取仅不带参数的方法
我正在做一个关于 Java 反射的学校作业。详情如下:
编写一个控制台程序,询问用户类名,加载 该类并创建它的实例。我们假设该类有 没有任何参数的构造函数。然后,程序打印出 创建对象的公共变量的名称和值,以及 还有不指定参数的公共方法的列表。这 程序应该让用户选择一个方法并执行该方法 创建的对象。之后,程序应该再次显示 公共变量及其值,并允许用户选择 方法等。使用以下课程来测试您的 实施:
公共类计数器 { 公共 int c; 公共无效增量(){C++; } 公共无效减量(){c--; } 公共无效重置(){c = 0; } }
我遇到的问题与以下句子有关:“不指定参数的公共方法列表”。有没有办法只列出没有参数的方法?我已经使用了 getMethods,但最终我从 Object 和 Class 超类中获取了很多带参数的方法。
例如,我编写的以下代码:
import java.lang.reflect.*;
import java.io.*;
public class Q1 {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What class would you like to run? ");
String className = reader.readLine();
Class c = Class.forName(className);
Object o = c.newInstance();
for (Field f : c.getFields())
System.out.println(f);
for (Method m : c.getMethods())
System.out.println(m);
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
输出以下内容:
您想参加什么课程?柜台
公共 int Counter.c
public void Counter.reset()
public void Counter.increment()
public void Counter.decrement()
public final native void java.lang.Object.wait(long) 抛出 java.lang.InterruptedException
public final void java.lang.Object.wait() 抛出 java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) 抛出 java.lang.InterruptedException
公共布尔 java.lang.Object.equals(java.lang.Object)
公共 java.lang.String java.lang.Object.toString()
公共本机 int java.lang.Object.hashCode()
公共最终本机java.lang.Class java.lang.Object.getClass()
公共最终本机无效java.lang.Object.notify()
公共最终本机无效java.lang.Object.notifyAll()
有没有办法只打印没有参数的内容?另外,我对作业细节的解释一开始就正确吗?或者“不指定参数的公共方法”这句话可能意味着其他意思,而我的想法完全错误?
I'm working on a school assignment about Java reflection. The details are below:
Write a console program that asks the user for a class name, loads
that class and creates an instance of it. We assume that the class has
a constructor without any parameters. Then, the program prints out the
names and values of the public variables of the created object, and
also a list of the public methods that do not specify a parameter. The
program should let the user choose a method and execute that method on
the created object. Afterwards, the program should again show the
public variables with their values and allow the user to choose a
method, and so on. Use the following class to test your
implementation:public class Counter { public int c; public void increment() { c++; } public void decrement() { c--; } public void reset() { c = 0; } }
The problem I am having has to do with the following sentence: "list of the public methods that do not specify a parameter". Is there a way to list only methods with no parameters? I have used getMethods but I end up getting a lot of methods from the Object and Class superclasses with parameters.
For example the following code that I have written:
import java.lang.reflect.*;
import java.io.*;
public class Q1 {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What class would you like to run? ");
String className = reader.readLine();
Class c = Class.forName(className);
Object o = c.newInstance();
for (Field f : c.getFields())
System.out.println(f);
for (Method m : c.getMethods())
System.out.println(m);
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Outputs the following:
What class would you like to run? Counter
public int Counter.c
public void Counter.reset()
public void Counter.increment()
public void Counter.decrement()
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Is there a way to get only the ones with no parameters to be printed? Also is my interpretation of the assignment details right in the first place? Or does the phrase "public methods that do not specify a parameter" possibly mean something else and I have entirely the wrong idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看过 Method 类的 API 了吗?有一个名为 getParameterTypes() 其中包含您正在寻找的答案,并且 API 明确说明了如果没有参数,将返回什么。只需在返回的方法上的 for 循环中调用它,您就应该像燧石一样。
Have you looked at the API for the Method class? There's a method called getParameterTypes() that has the answer for what you're looking for, and the API states explicitly what this will return if there are no parameters. Just call this in your for loop on the Methods returned and you should be in like flint.
只需使用 Method 类getParameterTypes 函数。如果返回值为 0,则该函数没有参数。 Java 文档的关键部分:
Just use the Method class' getParameterTypes function. If the return value is 0 then there are no parameters to that function. Key part from the Java doc: