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 女儿婚礼女婿祝福语简短