Java 通过反射动态执行方法

/ 语言 / 没有评论 / 1345浏览

    public static void main(String[] args) throws  Exception{




        Class clazz = Class.forName("com.mantis.hc.sale.dto.response.ExamAssistCustomerQueryRespDTO");
        Class clazz1 = ExamAssistCustomerQueryRespDTO.class;

        ExamAssistCustomerQueryRespDTO examAssistCustomerQueryRespDTO = new ExamAssistCustomerQueryRespDTO();
        examAssistCustomerQueryRespDTO.setAccount("qweqweqweqweqw");
         String  attachmenturl = (String) clazz.getMethod("getAccount").invoke(examAssistCustomerQueryRespDTO);//执行方法
        String  attachmenturl1 = (String) clazz1.getMethod("getAccount").invoke(examAssistCustomerQueryRespDTO);//执行方法

        System.out.println(attachmenturl);
        System.out.println(attachmenturl1);

    }