java反射机制

package com.yu.base;

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

public class ReflectDemo {
    public static void main(String[] args) {
        Person person = new Person(1,"张三",'男',22);
        //获取类的类对象
        Class<Person> personClass = Person.class;
        //判断对象是否为类对象的实例
        //System.out.println(personClass.isInstance(person));
        //获取类对象的类全名
        //System.out.println(personClass.getName());
        //获取全部自定义的方法
//        Method[] methods = personClass.getDeclaredMethods();
//        for (Method method : methods) {
//            System.out.println(method);
//        }
        //获取指定参数的构造方法
//        try {
//            Constructor<Person> constructor = personClass.getConstructor(int.class, String.class, char.class, int.class);
//            Person person1 = constructor.newInstance(2, "李四", '男', 30);
//            System.out.println(person1);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
//        try {
//            //获取共有字段
//            Field id1 = personClass.getField("id");
//            //获取私有字段
//            Field id = personClass.getDeclaredField("id");
//            //暴力修改私有字段
//            id.setAccessible(true);
//            id.set(person,3);
//            System.out.println(person);
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }

        try {
            //通过传入方法名,方法参数类型2个参数获取指定的方法s
            Method setName = personClass.getMethod("setName",String.class);
            //通过传入调用者和方法实参来调用方法,
            Object invoke = setName.invoke(person,"赵六");
            System.out.println(person);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }
}
class Person{
    private int id;
    private String name;
    private char sex;
    private int age;


    public int getId() {
        return id;
    }

    public Person() {
    }

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

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值