反射及通过发射获取方法和属性

获取class对象的三种方式:

package com.work.zh;

import com.work.week01.Person;

//反射  文件转对象的三种方法
public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException {
        //1.类.class
        Class<Person> personClass = Person.class;
        System.out.println(personClass);
        //2.Class.forName 通过类的全限定名字
        //来获取类所对应的class对象(需要抛异常)
        Class<?> aClass = Class.forName("com.work.week01.Person");
        System.out.println(aClass);
        //3.通过对象来创建class对象 new.对象.getClass()
        Class<? extends Person> aClass1 = new Person().getClass();
        System.out.println(aClass1);
    }
}

对Class对象调用的方法:

getConstructors() 返回包含一个数组 Constructor对象反射由此表示的类的所有公共构造方法

getDeclaredConstructors()返回一个反映 Constructor对象表示的类声明的所有 Constructor对象的数组

getConstructor(类<?>... parameterTypes)返回一个 Constructor对象,该对象反映 Constructor对象表示的类的指定的公共 函数。

getDeclaredConstructor(类<?>... parameterTypes)返回一个 Constructor对象,该对象反映 Constructor对象表示的类或接口的指定 函数。

对constructor调用的方法:

newInstance(Object...inintargs)使用此Constructor对象表示的构造函数,使用指定的 初始化参数 来创建和初始化 构造函数的声明类的新实例。

package com.qf.moxie3;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//通过反射获取构造方法
public class Demo01 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Person> cla = Person.class;
        //获取所有公开的
        Constructor<?>[] constructors = cla.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println("=================");
        //获取所有的
        Constructor<?>[] cost1 = cla.getDeclaredConstructors();
        for (Constructor<?> constructor : cost1) {
            System.out.println(constructor);
        }
        System.out.println("====================");
        //获取单个公开的
        Constructor<Person> constructor = cla.getConstructor(String.class);
        System.out.println(constructor);
        System.out.println("===================");
        //获取单个的私有的构造方法
        Constructor<Person> dec1 = cla.getDeclaredConstructor(String.class,int.class);
        System.out.println(dec1);
        dec1.setAccessible(true);
        Person person = (Person) dec1.newInstance("张三",15);
        System.out.println(person);
    }
}

获取Method(方法)对象

Class对象调用的

getMethods()返回包含一个数组方法 对象反射由此表示的类或者接口的所有公共方法 类对象,包括那些由类或接口和那些从超类和超接口继承的声明

getDeclaredMethods()返回包含一个数组 方法对象反射的类或接口的所有声明的方法,通过此表示 对象,包括公共,保护,默认(包)访问和私有方法,但不包括继承的方法。

getMethod(String name, 类<?>... parameterTypes)返回一个 方法对象,它反映此表示的类或接口的指定公共成员方法 对象

getDeclaredMethod(String name, 类<?>... parameterTypes)返回一个 方法对象,它反映此表示的类或接口的指定声明的方法 对象。

Method对象调用:

package com.qf.moxie3;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo03 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<Person> personClass = Person.class;
        //获取所有公开的方法 包括继承父类的
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("==============");
        //获取所有的方法 不包括父类的
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        System.out.println("==============");
        //获取单个的公开的方法
        Method method = personClass.getMethod("eat");
        System.out.println(method);
        System.out.println("==============");
        //获取单个的方法 也可以是私有的
        Method a = personClass.getDeclaredMethod("sleep", String.class);
        System.out.println(a);
        System.out.println("==============");
        //获取方法的名字
        String name = a.getName();
        System.out.println(name);
        System.out.println("==============");
        //获取方法的形参
        Class<?>[] parameterTypes = a.getParameterTypes();
        for (Class<?> parameterType : parameterTypes) {
            System.out.println(parameterType);
        }
        System.out.println("==============");
        //方法运行
        Person per = new Person();
        a.setAccessible(true);
        Object b = a.invoke(per, "睡大觉");
        System.out.println(b);
    }
}

获取Field对象(成员变量)

Class对象:

getFields()返回包含一个数组 Field对象反射由此表示的类或接口的所有可访问的公共字段(属性) 对象。

getDeclaredFields()返回的数组 Field对象反映此表示的类或接口声明的所有字段 对象。

getField(String name)返回一个 Field对象,它反映此表示的类或接口的指定公共成员字段 对象。

getDeclaredField(String name)返回一个 Field对象,它反映此表示的类或接口的指定已声明字段 对象。

Field对象的方法:

package com.qf.moxie3;
import java.lang.reflect.Field;
public class Demo02 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Class<Person> person = Person.class;

        Field[] fields = person.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("==========");

        Field[] declaredFields = person.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        System.out.println("============");
        //获取单个的成员变量
        Field name = person.getDeclaredField("name");
        System.out.println(name);
        //获取权限修饰符
        int modifiers = name.getModifiers();
        System.out.println(modifiers);
        //获取数据类型
        Class<?> type = name.getType();
        System.out.println(type);
        //获取成员变量的值
        Person person1 = new Person("zhangsan",15,45.8);
        name.setAccessible(true);//临时解除权限强转
        Object value = (String)name.get(person1);//获取值
        System.out.println(value);
        name.set(person1,"lisi");//赋值 改变
        System.out.println(person1);
        System.out.println(name.get(person1));

        Field age = person.getDeclaredField("age");
        System.out.println(age);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值