JAVA 中反射方法 reflection method

来源:互联网 发布:手机cad软件下载 编辑:程序博客网 时间:2024/05/18 15:07

JAVA 中反射出类型是比较简单的,这里记录的是已知类型和方法的字符串,如何反射出此类中对应的方法,

package com.huoli.loco._test;import java.lang.reflect.Method;public class TestTest {    public static void  main(String[] args) {        Test1 test = new Test1();        Class class1 = test.getClass();        try {            Method method = class1.getDeclaredMethod("setAppid", String.class);            method.invoke(test, "appioaiehfoiuaaodifhd");            System.out.println(test.getAppid());        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }     }}class Test1{    String appid;    public String getAppid() {        return appid;    }    public void setAppid(String appid) {        this.appid = appid;    }}
0 0