java使用反射执行方法,java反射执行,package cn.o
分享于 点击 26298 次 点评:77
java使用反射执行方法,java反射执行,package cn.o
package cn.outofmemory.snippets.core;import java.lang.reflect.Method;public class InvokeMethodWithReflection { public static void main(String[] args) throws Exception { StringBuilder sb = new StringBuilder(); sb.append("Java Code Geeks"); System.out.println("Initial: " + sb); // retrieve the method named "append" Method appendMethod = sb.getClass().getMethod("append", String.class); // invoke the method with the specified argument appendMethod.invoke(sb, "Java Examples & Code Snippets"); System.out.println("Final: " + sb); }}
输出:
Initial: Java Code GeeksFinal: Java Code GeeksJava Examples & Code Snippets
用户点评