Let's I have method
someMethod(int, String)
and I want to invoke it by method invoke(Object, Object[]) of class Method. 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. If your method is static you can pass null
instead of obj
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.