java中如何知道一个类是否有方法以及如何调用它
我需要知道java类是否有该方法
public double getValue()
(如果有)。我需要调用该方法。
抱歉,我忘了说这需要在运行时完成
I need to know if a java class has the method
public double getValue()
if there is a method. I need call the method.
Sorry, I forgot to say that this need to do at runtime
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Class.getMethod()
获取Method
对象,然后Method.call()
调用该方法。getMethod()
将抛出 < code>NoSuchMethodException 如果该类没有具有所请求名称和签名的方法。You could use
Class.getMethod()
to get theMethod
object, and thenMethod.call()
to invoke the method.getMethod()
will throwNoSuchMethodException
if the class doesn't have a method with the requested name and signature.您需要获取类,然后获取类中的方法。假设焦虑是你的目标。
You need to get the class, and then the methods from the class. Assume angst is your object.
您必须使用 java Reflection
You must use java Reflection