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 红包祝福语简短10字
18 周六早晨祝福语简短