1. package Test25_Demo.TestDemo2;/*
    2. @create 2020--12--18--10:14
    3. */
    4. import org.junit.Test;
    5. import java.lang.reflect.Constructor;
    6. import java.lang.reflect.InvocationTargetException;
    7. import java.lang.reflect.Method;
    8. /**
    9. * 反射处理静态方法
    10. */
    11. public class ReflectDemo3 {
    12. @Test
    13. public void test01() throws IllegalAccessException, InstantiationException {
    14. //静态方法的调用 - 普通
    15. int count = Student.getCount(10);
    16. System.out.println(count);
    17. }
    18. @Test
    19. public void test02() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    20. //利用反射机制来处理
    21. //1.创建Class
    22. String className = "Test25_Demo.TestDemo2.Student";
    23. Class clazz = Class.forName(className);
    24. //2.调用静态方法
    25. Method getCount = clazz.getMethod("getCount", int.class);
    26. //3.根据类型传参 - invoke方法表示映射,这里因为不需要实例,所以可以传参null
    27. Object returnValue = getCount.invoke(null, 2);
    28. System.out.println(returnValue);//20
    29. }
    30. }