Java高级特性之反射

概念定义

什么是反射?JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法,这种动态获取、调用对象方法的功能称为java语言的反射机制。
反射是通过Class对象(字节码文件),来知道某个类的所有属性和方法。也就是说通过反射我们可以获取构造器,对象,属性,方法。

执行流程

在这里插入图片描述
获取Class对象的方式
Class student = Student.class; //方式一
Class<?> forName = Class.forName(“com.test.utils.reflection.Student”); //方式二
通过Class对象获取类加载器
下面的这个类创建了多个构造方法

public class Student {
    Student(String name) {
        System.out.println("用default修饰的Student的含有一个String参数的构造器:"+name);
    }
    public Student() {
        System.out.println("用public修饰的Student的无参构造器");
    }
    public Student(String name,int age) {
        System.out.println("用public修饰的Student的含有两个参数的构造器:"+name+age);
    }
    public Student(boolean sex) {
        System.out.println("用public修饰的Student的含有一个参数的构造器:"+sex);
    }
    protected Student(int age) {
        System.out.println("用protected修饰的Student的含有一个参数的构造器:"+age);
    }
    private Student(String name,int age,boolean sex) {
        System.out.println("用private修饰的Student的含有三个参数的构造器:"+name+age+sex);
    }
    private void test(String name) {
        System.out.println("方法调用成功:"+name);
    }
}
public static void main(String[] args) throws Exception {
        // 生成对应的类
        Class student = Student.class;
        System.out.println("输出所有的public构造方法");
        // 获取当前类的所有的public构造方法
        Constructor<?>[] constructors = student.getConstructors();
        for (Constructor constructor: constructors) {
            System.out.println(constructor);
        }

        System.out.println("输出所有的构造方法");
        // 获取当前类的所有的构造方法
        Constructor<?>[] declaredConstructors = student.getDeclaredConstructors();
        for (Constructor constructor: declaredConstructors) {
            System.out.println(constructor);
        }

        // 调用这个构造方法 private Student(String name,int age,boolean sex)
        Constructor constructor = student.getDeclaredConstructor(String.class,int.class,boolean.class);
        // 该方法将忽略构造器的修饰符,即可在其他package下通过反射调用private的构造方法
        constructor.setAccessible(true);
        // 实例化一个对象
        Student studentInstance = (Student) constructor.newInstance("11",2,true);
        System.out.println("Student对象实例:" + studentInstance);

        System.out.println("获取所有public的方法");
        // 获取当前类的所有public的方法
        Method[] declaredMethods = student.getDeclaredMethods();
        for (Method method:declaredMethods) {
            System.out.println(method);
        }
        System.out.println("获取所有的方法");
        // 获取所有的方法
        Method[] methods = student.getMethods();
        for (Method method:methods) {
            System.out.println(method);
        }
    }

执行结果

输出所有的public构造方法
public com.test.utils.reflection.Student(boolean)
public com.test.utils.reflection.Student()
public com.test.utils.reflection.Student(java.lang.String,int)
输出所有的构造方法
private com.test.utils.reflection.Student(java.lang.String,int,boolean)
protected com.test.utils.reflection.Student(int)
public com.test.utils.reflection.Student(boolean)
com.test.utils.reflection.Student(java.lang.String)
public com.test.utils.reflection.Student()
public com.test.utils.reflection.Student(java.lang.String,int)
用private修饰的Student的含有三个参数的构造器:112true
Student对象实例:com.test.utils.reflection.Student@2280cdac
获取所有public的方法
public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception
private void com.test.utils.reflection.Student.test(java.lang.String)
获取所有的方法
public static void com.test.utils.reflection.Student.main(java.lang.String[]) throws java.lang.Exception
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

image

最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:756584822

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值