栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA反射

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA反射

import org.junit.Test;


public class ClassDemo {
    
    @Test
    public void test() throws ClassNotFoundException {
        //(1)类型名.class
        Class clazz = Person.class;

        //(2)对象.getClass()
        Person p = new Person();
        Class clazz2 = p.getClass();

        //(3)Class.forName(类型全名称)
        //全类名 :包含包名在内的类的全名称
        Class clazz3 = Class.forName("com.atguigu.java2.Person");

        //(4)ClassLoader的类加载器对象.loadClass(类型全名称)
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class clazz4  = classLoader.loadClass("com.atguigu.java2.Person");

        System.out.println(clazz == clazz2 && clazz3 == clazz4 && clazz == clazz3);
    }
}

 

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Arrays;

public class ClassDemo2 {

    
    @Test
    public void test(){
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取方法-本类及父类中所有public修饰的方法
        Method[] methods = clazz.getMethods();
        //3.遍历
        for (Method method : methods) {
            System.out.println(method);
        }

        System.out.println("=================================");
        //获取本类中所有的方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }

    }

    
    @Test
    public void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Student s = new Student();
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取指定的方法--只能获取public修饰的方法
        
        Method method = clazz.getMethod("studentPublicDemo", String.class);
        //3.调用方法
        
        method.invoke(s,"aaa");
    }
    @Test
    public void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Student s = new Student();
        //1.获取Class对象
        Class clazz = Student.class;
        //2.调用指定方法---可以获取任意权限修饰符修饰的方法
        
        Method studentPrivateSay = clazz.getDeclaredMethod("studentPrivateSay",int.class);
        //3.允许访问--授权
        studentPrivateSay.setAccessible(true);
        //4.调用方法
         
        studentPrivateSay.invoke(s,1001);
    }

    
    @Test
    public void test4(){
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取属性--获取本类及父类中public修饰的属性
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("=============================================");
        //获取属性-获取本类中所有的属性
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
    }

    
    @Test
    public void test5() throws NoSuchFieldException, IllegalAccessException {
        Student s = new Student();
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取指定的属性---获取public修饰的属性
        Field studentPublicName = clazz.getField("studentPublicName");
        //3.给属性赋值
        
        studentPublicName.set(s,"longge");

        s.show();
    }
    @Test
    public void test6() throws NoSuchFieldException, IllegalAccessException {
        Student s = new Student();
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取指定的属性-任意权限修饰符修饰的属性都可以
        Field studentPrivateId = clazz.getDeclaredField("studentPrivateId");
        //3.允许访问
        studentPrivateId.setAccessible(true);
        //4.给属性赋值
         
        studentPrivateId.setInt(s,1001);

        s.show();
    }

    
    @Test
    public void test7() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //1.获取Class对象
        Class clazz = Employee.class;
        //2.获取指定的构造器
        
        //Constructor constructor = clazz.getConstructor();
        //3.创建对象
        
        //Employee e = (Employee) constructor.newInstance();
        //e.show();

        //注意:如果要通过空参构造器创建对象可以直接使用Class的对象调用newInstance方法
        clazz.newInstance();
    }

    
    @Test
    public void test8(){
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取父类的运行时类
        Class superclass = clazz.getSuperclass();
    }

    
    @Test
    public void test9() throws NoSuchMethodException {
        //1.获取Class的对象
        Class clazz = Employee.class;
        //2.获取注解--获取类上的注解
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        System.out.println("=====================================");
        //3.获取指定结构上的注意:①先获取那个结构 ②再获取上面的注解
        //获取方法
        Method show = clazz.getMethod("show");
        //获取方法上的注解
        Annotation[] annotations1 = show.getAnnotations();
        for (Annotation annotation : annotations1) {
            System.out.println(annotation);
        }
    }


    
    @Test
    public void test10(){
        //1.获取Class对象
        Class clazz = Student.class;
        //2.获取本类中所有的属性
        Field[] declaredFields = clazz.getDeclaredFields();

        for (Field declaredField : declaredFields) {

            //获取注解
            declaredField.getAnnotations();

            //获取属性的权限修饰符
            int modifiers = declaredField.getModifiers();
            //通过Modifier的toString方法将数字转成对应的名字
            System.out.print(Modifier.toString(modifiers) + " ");

            //获取属性的类型
            Class type = declaredField.getType();
            System.out.print(type + " ");

            //获取属性的名字
            System.out.print(declaredField.getName() + ";");


            System.out.println();
        }
    }

    
    @Test
    public void test11(){
        //1.获取Class的对象
        Class clazz = Student.class;
        //2.获取本类中所有方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            //获取方法的权限修饰符
            System.out.print(Modifier.toString(declaredMethod.getModifiers()) + " ");
            //获取方法的返回值类型
            System.out.print(declaredMethod.getReturnType() + " ");
            //获取方法的名字
            System.out.print(declaredMethod.getName() + " ");
            //获取方法的形参的类型
            System.out.print("(");
            Class[] classes = declaredMethod.getParameterTypes();
            System.out.print(Arrays.toString(classes));
            System.out.print(")");

            System.out.println();
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号