简体   繁体   English

Java类方法,方法invoke()和原始类型

[英]Java Class Method, method invoke() and primitive types

Let's I have method 我有方法

someMethod(int, String)

and I want to invoke it by method invoke(Object, Object[]) of class Method. 我想通过类Method的方法invoke(Object,Object [])来调用它。 Can I do it? 我可以做吗? If I can then how? 如果可以的话怎么办?

您可以使用invoke(Instance of Object containing Method, 5, "foo")

You can do it as following: 您可以按照以下步骤进行操作:

MyClass.class.getMethod(int.class, String.class).invoke(obj, 1, "hello")

where obj is a instance of your class. 其中obj是您的类的实例。 If your method is static you can pass null instead of obj . 如果您的方法是静态的,则可以传递null而不是obj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM