打印类Print.java

package com.test.reflct;

public class Print {

    public static void print(String str){
        System.out.println("打印测试"+str);
    }
}

调用类

package com.test.reflct;

import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) {

        try {
            //加载类
            Class<?> printClass =  Class.forName("com.test.reflct.Print");
            //获取方法
            Method printMethod = printClass.getMethod("print",String.class);
            //调用
            printMethod.invoke(printClass.newInstance(),"123");
        } catch (Exception e) {
            e.printStackTrace();
        }    

    }

}

相关文章:

  • 2021-11-26
  • 2021-11-12
  • 2021-10-27
  • 2021-06-29
猜你喜欢
  • 2021-08-06
  • 2021-08-06
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
相关资源
相似解决方案