反射

目录

反射

一、J V M类加载流程和内存结构

1、jvm加载流程图

2、获取Class类对象的三种方式【应用】

3、反射获取构造方法并使用【应用】

  3.1 Class类获取获取构造方法对象的方法

  3.2 Constructor类用于创建对象的方法

二、反射创建对象和普通方法创建对象的区别

1、普通方法创建对象时在编译期间,打开和检查class文件,创建对象;反射创建在运行时加载class文件调用方法创建对象。

2、两者生成对象的步骤

总结


 



反射

反射机制

是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用 它的任意属性和方法; 这种动态获取信息以及动态调用对象方法的功能称为Java语言的反射机制。(俗话说也就是通过一些手段获取任一方法对象等,即使它是私有的方法)


提示:以下是本篇文章正文内容,下面案例可供参考

一、J V M类加载流程和内存结构

1、jvm加载流程图

2、获取Class类对象的三种方式【应用】

三种方式分类

类名.class属性

对象名.getclass()方法

class.forname(全类名)方法

public class Student {
private String name;
private int age;
//私有的有参构造方法
private Student(String name) {
System.out.println("name的值为:" + name);
System.out.println("private...Student...有参构造方法");
}
//公共的无参构造方法
public Student() {
System.out.println("public...Student...无参构造方法");
}
//公共的有参构造方法
public Student(String name, int age) {
System.out.println("name的值为:" + name + "age的值为:" + age);
System.out.println("public...Student...有参构造方法");
}
}
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//1.Class类中的静态方法forName("全类名")
//全类名:包名 + 类名
Class clazz = Class.forName("com.itheima.myreflect2.Student");
System.out.println(clazz);
//2.通过class属性来获取
Class clazz2 = Student.class;
System.out.println(clazz2);
//3.利用对象的getClass方法来获取class对象
//getClass方法是定义在Object类中.
Student s = new Student();
Class clazz3 = s.getClass();
System.out.println(clazz3);
System.out.println(clazz == clazz2);
System.out.println(clazz2 == clazz3);
}
}

 

3、反射获取构造方法并使用【应用】

3.1 Class类获取获取构造方法对象的方法

  1. Constructor[] getConstructors() 返回所有公共构造方法对象的数组
  2. Constructor[] getDeclaredConstructors() 返回所有构造方法对象的数组
  3. Constructor getConstructor(Class... parameterTypes) 返回单个公共构造方法对象
  4. Constructor getDeclaredConstructor(Class... parameterTypes) 返回单个构造方法对象
  5. private static void method4() throws ClassNotFoundException, NoSuchMethodException {
    // Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes):
    // 返回单个构造方法对象
    //1.获取Class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    Constructor constructor = clazz.getDeclaredConstructor(String.class);
    System.out.println(constructor);
    }
    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
    // Constructor<T> getConstructor(Class<?>... parameterTypes):
    // 返回单个公共构造方法对象
    //1.获取Class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    //小括号中,一定要跟构造方法的形参保持一致.
    Constructor constructor1 = clazz.getConstructor();
    System.out.println(constructor1);
    Constructor constructor2 = clazz.getConstructor(String.class, int.class);
    System.out.println(constructor2);
    //因为Student类中,没有只有一个int的构造,所以这里会报错.
    Constructor constructor3 = clazz.getConstructor(int.class);
    System.out.println(constructor3);
    }
    private static void method2() throws ClassNotFoundException {
    // Constructor<?>[] getDeclaredConstructors():
    // 返回所有构造方法对象的数组
    //1.获取Class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    Constructor[] constructors = clazz.getDeclaredConstructors();
    for (Constructor constructor : constructors) {
    System.out.println(constructor);
    }
    }
    private static void method1() throws ClassNotFoundException {
    // Constructor<?>[] getConstructors():
    // 返回所有公共构造方法对象的数组
    //1.获取Class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    Constructor[] constructors = clazz.getConstructors();
    for (Constructor constructor : constructors) {
    System.out.println(constructor);
    }
    }

    3.2 Constructor类用于创建对象的方法

  6. T newInstance(Object...initargs) 根据指定的构造方法创建对象
  7. setAccessible(boolean flag) 设置为true,表示取消访问检查
  8. // Student类同上一个示例,这里就不在重复提供了
    public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException,
    NoSuchMethodException, IllegalAccessException, InvocationTargetException,
    InstantiationException {
    //T newInstance(Object... initargs):根据指定的构造方法创建对象
    //method1();
    //method2();
    //method3();
    //method4();
    }
    private static void method4() throws ClassNotFoundException, NoSuchMethodException,
    InstantiationException, IllegalAccessException, InvocationTargetException {
    //获取一个私有的构造方法并创建对象
    //1.获取class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    //2.获取一个私有化的构造方法.
    Constructor constructor = clazz.getDeclaredConstructor(String.class);
    //被private修饰的成员,不能直接使用的
    //如果用反射强行获取并使用,需要临时取消访问检查
    constructor.setAccessible(true);
    //3.直接创建对象
    Student student = (Student) constructor.newInstance("zhangsan");
    System.out.println(student);
    }
    private static void method3() throws ClassNotFoundException, InstantiationException,
    IllegalAccessException {
    //简写格式
    //1.获取class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    //2.在Class类中,有一个newInstance方法,可以利用空参直接创建一个对象
    Student student = (Student) clazz.newInstance();//这个方法现在已经过时了,了解一下
    System.out.println(student);
    }
    private static void method2() throws ClassNotFoundException, NoSuchMethodException,
    InstantiationException, IllegalAccessException, InvocationTargetException {
    //1.获取class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    //2.获取构造方法对象
    Constructor constructor = clazz.getConstructor();
    //3.利用空参来创建Student的对象
    Student student = (Student) constructor.newInstance();
    System.out.println(student);
    }
    private static void method1() throws ClassNotFoundException, NoSuchMethodException,
    InstantiationException, IllegalAccessException, InvocationTargetException {
    //1.获取class对象
    Class clazz = Class.forName("com.itheima.myreflect3.Student");
    //2.获取构造方法对象
    Constructor constructor = clazz.getConstructor(String.class, int.class);
    //3.利用newInstance创建Student的对象
    Student student = (Student) constructor.newInstance("zhangsan", 23);
    System.out.println(student);
    }
    }

     

二、反射创建对象和普通方法创建对象的区别

1、普通方法创建对象时在编译期间,打开和检查class文件,创建对象;反射创建在运行时加载class文件调用方法创建对象。

2、两者生成对象的步骤


总结

  1. 采用反射机制来实现一个工具BeanUtils, 可以将一个对象属性相同的值赋值给另一个对象。
  2. 获取class对象 三种方式: Class.forName(“全类名”), 类名.class, 对象名.getClass()。
  3. 获取里面的构造方法对象。getConstructor (Class... parameterTypes) getDeclaredConstructor (Class... parameterTypes)
  4. 如果是public的,直接创建对象 newInstance(Object... initargs) 如果是非public的,需要临时取消检查,然后再创建对象 setAccessible(boolean) 暴力反射。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值