2.2 Java反射

本文介绍了Java中的反射机制,包括通过Class对象获取类信息,以及对构造方法、成员变量和成员方法的动态访问。文中提供了多种获取Class对象的方式,如通过对象的getClass()方法、类的.class属性和Class.forName()。此外,还展示了如何调用无参和含参数的构造方法,访问公有和私有的成员变量,以及执行公有和私有的成员方法。
摘要由CSDN通过智能技术生成

1.简介
(1).概念
反射是指程序在运行时,可以动态获取对象所有成员和方法,并且对其进行访问或修改的机制。

(2).Class类

  • 类是用来描述事物的,那么描述类的类就是Class类。
  • Class类的对象只能由JVM创建,JVM中只有唯一一个和类相对应的Class对象。

(3).reflect类库

  • Field:表示类中的成员变量
  • Method:表示类中的成员方法
  • Constructor:表示类中的构造方法

(4).演示类

public class Student {
    private String name;

    int age;

    public Student() {
        System.out.println("constructor run without params");
    }

    private Student(String name) {
        this.name = name;
        System.out.println("public constructor params run with name:" + this.name);
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("public constructor params run with name:" + this.name + ",age:" + this.age);
    }

    public void show() {
        System.out.println("public show run");
    }

    private void show1() {
        System.out.println("private show run");
    }

    public void fun(String name) {
        System.out.println("fun run with name:" + name);
    }

    public static void staticMethod() {
        System.out.println("staticMethod run");
    }

	@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.获取Class对象的三种方式
(1).getClass()

public class ReflectTarget {
    public static void main(String[] args) {
        ReflectTarget reflectTarget = new ReflectTarget();
        Class reflectTargetClass = reflectTarget.getClass();
        System.out.println(reflectTargetClass.getName());
    }
}

(2).class属性

public class ReflectTarget {
    public static void main(String[] args) {
        Class reflectTargetClass = ReflectTarget.class;
        System.out.println(reflectTargetClass.getName());
    }
}

(3).forName()

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException {
        Class reflectTargetClass = Class.forName("com.steven.reflect.ReflectTarget");
        System.out.println(reflectTargetClass.getName());
    }
}

3.获取构造方法
(1).无参

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Constructor constructor = studentClass.getConstructor();
        Student student = (Student)constructor.newInstance();
        student.show();
    }
}
constructor run without params
public show run

(2).公有含参数

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Constructor constructor = studentClass.getDeclaredConstructor(String.class,int.class);
        Student student = (Student)constructor.newInstance("steven",30);
        System.out.println(student);
    }
}
public constructor params run with name:steven,age:30
Student{name='steven', age=30}

(3).私有含参数

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Constructor constructor = studentClass.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        Student student = (Student)constructor.newInstance("steven");
        System.out.println(student);
    }
}
public constructor params run with name:steven
Student{name='steven', age=0}

4.获取成员变量
(1).公有成员变量

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Student student = (Student)studentClass.newInstance();
        Field field = studentClass.getDeclaredField("age");
        field.set(student,30);
        System.out.println(student);
    }
}
constructor run without params
Student{name='null', age=30}

(2).私有成员变量

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Student student = (Student)studentClass.newInstance();
        Field field = studentClass.getDeclaredField("name");
        field.setAccessible(true);
        field.set(student,"steven");
        System.out.println(student);
    }
}
constructor run without params
Student{name='steven', age=0}

5.获取成员方法
(1).公有成员方法

Class studentClass = Class.forName("com.steven.reflect.Student");
        Student student = (Student)studentClass.newInstance();
        Method method = studentClass.getDeclaredMethod("fun",String.class);
        method.invoke(student,"steven");
constructor run without params
fun run with name:steven

(2).私有成员方法

public class ReflectTarget {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
        Class studentClass = Class.forName("com.steven.reflect.Student");
        Student student = (Student)studentClass.newInstance();
        Method method = studentClass.getDeclaredMethod("show1");
        method.setAccessible(true);
        method.invoke(student);
    }
}
constructor run without params
private show run
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值