Java调用方法
示例
使用反射,可以在运行时调用对象的方法。
该示例显示了如何调用String对象的方法。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
String s = "你好,世界!";
//没有参数的方法
//调用s.length()
Method method1 = String.class.getMethod("length");
int length = (int) method1.invoke(s); // variable length contains "12"
//带参数的方法
//调用s.substring(6)
Method method2 = String.class.getMethod("substring", int.class);
String substring = (String) method2.invoke(s, 6); // variable substring contains "World!"
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短