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