Java反射(一)

通过反射获取Class
方法一

package reflectiondemo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class test {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {


//        反射获取class
        Class<?> aClass = Class.forName("java.lang.String");
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods){
            System.out.println(method);
        }

    }
}

方法二

        String str = "abcd";
        Class<?> aclass = str.getClass();
        Method[] methods = aclass.getDeclaredMethods();
        for(Method method : methods){
            System.out.println(method);
        }

方法三

        Class<?> aclass = String.class;
        Method[] methods = aclass.getDeclaredMethods();
        for(Method method : methods) {
            System.out.println(method);
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容