反射基本应用

前言

想了解更多java知识,推荐 B站 宝藏男孩遇见狂神说

ClassLoader

public abstract class ClassLoader
extends Object

  • 类加载器是负责加载类的对象。
  • ClassLoader类是一个抽象类。
  • 作用就是根据一个指定的类的全限定名,找到对应的Class字节码文件,然后加载它转化成一个java.lang.Class类的一个实例。

三种类加载器

  • 系统类加载器(Application ClassLoader)

系统默认的类加载器,负责加载用户类路径(classpath)下的类库。

  • 扩展类加载器(Extendsion ClassLoader)

负责加载\lib\ext目录下的类库,用来加载java的扩展库,开发者可以直接使用这个类加载器.

  • 根类加载器(Bootstrap ClassLoader)

负责将\lib目录下的类库加载到虚拟机内存中,用来加载java的核心库 rt.jar,
不能被java程序直接调用,代码是使用C++编写的.是虚拟机自身的一部分.


//       获取系统类加载器
        ClassLoader system = ClassLoader.getSystemClassLoader();
        System.out.println("获取系统类加载器"+system);


//        获取系统类加载器的父类----扩展类加载器
        ClassLoader parent = system.getParent();
        System.out.println("获取系统类加载器的父类----扩展类加载器"+parent);


//      获取扩展类加载器的父类---根加载器(是c/c++语言开发,java无法获取)
        ClassLoader root = parent.getParent();
        System.out.println("获取扩展类加载器的父类---根加载器(是c/c++语言开发,java无法获取)"+root);



//        测试当前类的加载器
        ClassLoader test = Class.forName("cn.ma.reflect.Test_1").getClassLoader();
        System.out.println("测试当前类的加载器"+test);


//输出结果

获取系统类加载器sun.misc.Launcher$AppClassLoader@18b4aac2
获取系统类加载器的父类----扩展类加载器sun.misc.Launcher$ExtClassLoader@1540e19d
获取扩展类加载器的父类---根加载器(是c/c++语言开发,java无法获取)null
测试当前类的加载器sun.misc.Launcher$AppClassLoader@18b4aac2

Field获取类的属性

  • getFields():获得某个类的所有(public)的字段,也包括父类中的字段。
  • getDeclaredFields():获得某个类的所有声明的字段,但是不包括父类的申明字段。
  • getMethod(“setName”, String.class);获取指定类的setName方法

public Method getMethod(String name, Class<?>… parameterTypes)

  • 反射获取类的属性、方法、构造器

     获取系统类加载器可以加载的路径,类在此路径可以加载
 System.out.println(System.getProperty("java.class.path"));




 Class cla = Class.forName("cn.ma.reflect.User");

        System.out.println("类的包名+类名:"+cla.getName());
        System.out.println("获得类名:"+cla.getSimpleName());

        System.out.println("*************获得该类的属性************************");

        //        只能获得public属性
        Field[] fields = cla.getFields();

//        找到全部的属性
        fields = cla.getDeclaredFields();
        for(Field field:fields){
            System.out.println(field);
        }

//        获取指定属性的值
        Field name = cla.getDeclaredField("sex");
        System.out.println(name);

        System.out.println("----------------获得本类及其父类的全部public方法--------------------");
        Method[] methods = cla.getMethods();
        for(Method m:methods){
            System.out.println(m);
        }

        System.out.println("---------------------获得本类的所有方法(包含private方法)-----------------------------------------");
        methods = cla.getDeclaredMethods();
        for(Method method:methods){
            System.out.println(method);
        }


        System.out.println("------------------------------获取指定方法------------------------------------------------");
        Method getname = cla.getMethod("getName");
        System.out.println(getname);

        Method setName = cla.getMethod("setName", String.class);
        System.out.println(setName);

        System.out.println("------------------------------构造器的获取-------------------------------------------------------");

        Constructor[] constructors = cla.getConstructors();
        for(Constructor c:constructors){ //获得本类及其父类的构造器
            System.out.println(c);
        }


        Constructor[] constructors1 = cla.getDeclaredConstructors();
        for(Constructor c:constructors){ //获得本类的
            System.out.println(c);
        }


  • 反射动态创建对象
  Class c = Class.forName("cn.ma.reflect.User");

//         通过反射动态创建对象
        //默认调用无参构造器,没有会报错
        User user = (User) c.newInstance();
        System.out.println(user);


        // 根据构造器创建对象
        Constructor constructor = c.getDeclaredConstructor(String.class,int.class,String.class);
        User user1 = (User) constructor.newInstance("mmc",199700,"male");
        System.out.println(user1);


//        用反射调用普通方法
        User user2 = (User)c.newInstance();
//        获取指定方法
        Method setName = c.getMethod("setName", String.class);


//        invoke方法(实例对象,"方法的值"):激活
        setName.invoke(user2,"王麻子");
        System.out.println(user2.getName());


        System.out.println("------------------通过反射操作属性---------------------------------");

        User user3 =(User) c.newInstance();
        Field name = c.getDeclaredField("name");

//        setAccessible(true)表示关闭安全检查权限,默认是false
        //设置为true可以访问 非public属性/方法
//这里如果为默认状态,则无法访问name属性
        name.setAccessible(true);
        name.set(user3,"张撒盐哥");
        System.out.println(user3.getName());
  • 用反射调用注解
package cn.ma.reflect;

import java.lang.annotation.*;
import java.lang.reflect.Field;

//通过反射调用注解
public class Test_6 {

    public static void main(String[] args) throws Exception {

        Class c = Class.forName("cn.ma.reflect.Student");

//        获取Student类的注解
        Annotation[] annotations = c.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

//   获取注解的value值
        Db_Student stu = (Db_Student)c.getAnnotation(Db_Student.class);
        System.out.println(stu.value());

//        获取name属性,取到属性上的注解的参数信息
       Field name = c.getDeclaredField("name");
        Name annotation = name.getAnnotation(Name.class);
        System.out.println(annotation.value());
        System.out.println(annotation.type());
        System.out.println(annotation.len());
    }
}

@Db_Student("db_student")
class Student{

    @Name(value = "mmc",type="varchar",len=20)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Db_Student{
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Name{
    String value();
    String type();
    int len();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值