欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java使用反射执行方法,java反射执行,package cn.o

来源: javaer 分享于  点击 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
相关栏目:

用户点评