74.反射-获取方法

使用反射机制获取方法

import java.lang.reflect.Method;

public class GetMethodDemo {
    
    public static void main(String[] args) throws Exception {
        //1.获得字节码对象
        Class clz = Class.forName("com.java520.class01.Person");

        //2.获取公共无参方法
        Method m = clz.getMethod("eat");
        System.out.println(m);

        //3.无视修饰符获取无参方法
        Method m1 = clz.getDeclaredMethod("eat", String.class);
        System.out.println(m1);

        //4.无视修饰符获得所有方法
        Method[] ms = clz.getDeclaredMethods();
        for (Method method : ms) {
            System.out.println(method);
        }

    }
    
}

你可能感兴趣的:(74.反射-获取方法)