剑道第一仙

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Java中反射获取成员变量、构造方法、成员方法及类名

转:https://blog.csdn.net/HeZhiYing_/article/details/101600520

都说反射是框架的灵魂,但是反射到底是啥呢,今天就聊聊反射的基础操作,也是必备操作。

反射机制是在程序运行时,对任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能调用他的任意一个属性和方法,将类的各个组成部分封装成对象,这就是反射机制。也就是说一个程序,在刚写好时是一个.java文件,当你编译之后,此时会生成.class字节码文件,这个时候的文件都是在硬盘中存储的,而并未加载到内存中,要想把硬盘中的字节码文件加载到内存,也就是类加载器(ClassLoader),可以把字节码文件加载到内存中,在内存中有一个Class类对象用来描述.class字节码文件,也就创建出来了我们经常见到的Person p = new Person();

在这里演示一下Class类对象的方法,首先我们创建一个Person类

复制代码
public class Person {
    private String name;
    private int age;

    public String a;
    protected String b;
    String c;
    private String d;

    public Person(){
    }

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", a='" + a + '\'' +
                ", b='" + b + '\'' +
                ", c='" + c + '\'' +
                ", d='" + d + '\'' +
                '}';
    }

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

    public void eat(String food){
        System.out.println("eat..."+food);
    }
}
复制代码

一、获取成员变量

在这里可以获取成员变量,然后能够对成员变量进行赋值和获取它的值

复制代码
import java.lang.reflect.Field;

public class ReflectDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //获取Person的Class对象
        Class personClass = Person.class;
        //获取所有的public修饰的成员变量
        Field[] fields = personClass.getFields();
        for(Field field : fields){
            System.out.println(field);
        }

        //获取单个public修饰的成员变量
        Field a = personClass.getField("a");
        //获取成员变量a的值
        Person p = new Person();
        Object value = a.get(p);
        System.out.println(value);

        //设置a的值
        a.set(p,"张三");
        System.out.println(p);

        //获取所有的成员变量,不考虑修饰符
        Field[] declaredFields = personClass.getDeclaredFields();
        for(Field declaredField : declaredFields){
            System.out.println(declaredField);
        }
        //获取单个的成员变量
        Field d = personClass.getDeclaredField("d");
        //由于d是私有的,所以需要忽略访问权限修饰符的安全检查
        d.setAccessible(true);//暴力反射
        Object value2 = d.get(p);
        System.out.println(value2);

    }

}
复制代码

 

二、获取构造方法

可以获取有参构造和无参构造,有参构造可以设置参数

复制代码
package com.zhiying;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo1 {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Person的Class对象
        Class personClass = Person.class;
        //获取构造方法,这里以有参构造为例
        Constructor constructor = personClass.getConstructor(String.class,int.class);
        System.out.println(constructor);
        //创建对象
        Object person = constructor.newInstance("张三",20);
        System.out.println(person);
        //无参构造
        Constructor constructor1 = personClass.getConstructor();
        System.out.println(constructor1);
        Object person1 = constructor1.newInstance();
        System.out.println(person1);
    }
}
复制代码

 

三、获取成员方法及类名

可以获取成员方法,有参的可以设置参数,其中包括Object中的方法,获取类的时候,getName()方法

复制代码
package com.zhiying;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo2 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        //获取Person的Class对象
        Class personClass = Person.class;

        //获取指定方法名称
        //无参
        Method eatMethod = personClass.getMethod("eat");
        Person p = new Person();
        eatMethod.invoke(p);
        //有参
        Method eatMethod1 = personClass.getMethod("eat", String.class);
        eatMethod1.invoke(p,"饭");

        //获取所有public修饰的方法及方法名称,其中包括Object方法
        Method[] methods = personClass.getMethods();
        for(Method method : methods){
            System.out.println("我是方法:"+method);
            System.out.println("我是方法名:"+method.getName());
        }

        //获取类名
        String className = personClass.getName();
        System.out.println("我是类名:"+className);

    }
}
复制代码

 

 

 

posted on   剑道第一仙  阅读(763)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示