JAVA反射(二)

package cn.foxsand.day02;

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

/**
 * 应用反射的API,获取类的信息(名称、属性、方法、构造器)
 */
public class Demo02 {
    public static void main(String[] args) {
        String path = "cn.foxsand.day02.UserBean";
        try {
            Class<?> clazz1 = Class.forName(path);

            // 获取类的名称
            System.out.println(clazz1.getName());       //获取全路径
            System.out.println(clazz1.getSimpleName()); //仅获取类名

            // 获取属性
            /**
             * Returns an array containing {@code Field} objects reflecting all
             * the accessible public fields of the class or interface represented by
             * this {@code Class} object.
             * 仅返回public属性
             */
            Field[] fields = clazz1.getFields();
            for (int i = 0; i < fields.length; i++) {
                System.out.println(fields[i].getName());
            }
            /**
             * Returns an array of {@code Field} objects reflecting all the fields
             *  declared by the class or interface represented by this
             * {@code Class} object. This includes public, protected, default
             * (package) access, and private fields, but excludes inherited fields.
             * 返回所有属性(继承的属性例外)
             */
            fields = clazz1.getDeclaredFields();
            for (Field f:fields
                 ) {
                System.out.println("属性:" + f.getName());
            }

            Field f1 = clazz1.getDeclaredField("age");
            System.out.println(f1.getName());

            // 获取方法
            Method[] methods = clazz1.getDeclaredMethods();
            for (Method m:methods
                 ) {
                System.out.println(m.getName());
            }
            methods = clazz1.getMethods();
            for (Method m:methods
                 ) {
                System.out.println("方法:" + m.getName());
            }

            Method m1 = clazz1.getDeclaredMethod("getAge");
            System.out.println(m1.getName());
            m1 = clazz1.getDeclaredMethod("setAge", int.class);
            System.out.println(m1.getName());
            // 获取构造器
            Constructor[] constructors = clazz1.getDeclaredConstructors();
            for (Constructor c:constructors
                 ) {
                System.out.println("构造器:" + c);
            }

            Constructor c = clazz1.getDeclaredConstructor(int.class , String.class , int.class);
            System.out.println(c);
        }catch (Exception e1){
            e1.printStackTrace();
        }
    }
}
package cn.foxsand.day02;

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

/**
 * 通过反射API,动态的操作:构造器、方法、属性
 */
public class Demo03 {
    public static void main(String[] args) {
        String path = "cn.foxsand.day02.UserBean";
        try {
            Class<UserBean> clazz1 = (Class<UserBean>)Class.forName(path);
            // 通过动态调用构造方法,构造对象
            // 1.调用Bean的无参构造器创建对象
            UserBean userBean = clazz1.newInstance();
            System.out.println(userBean);

            // 2.调用指定的有参构造器创建对象
            Constructor<UserBean> c1 = clazz1.getDeclaredConstructor(int.class , String.class , int.class);
            userBean = c1.newInstance(100 , "你好" , 10);
            System.out.println(userBean);

            // 通过反射API调用方法
            UserBean u2 = clazz1.newInstance();
            Method method = clazz1.getDeclaredMethod("setName", String.class);
            method.invoke(u2 , "张三");
            System.out.println(u2);

            // 通过反射API操作属性
            Field f = clazz1.getDeclaredField("name");
            /**
             * * Set the {@code accessible} flag for this object to
             *  the indicated boolean value.  A value of {@code true} indicates that
             *  the reflected object should suppress Java language access
             *  checking when it is used.  A value of {@code false} indicates
             *  that the reflected object should enforce Java language access checks.
             *  为ture时,私有属性也可以set。
             *  false时,不能操作私有属性
             */
            f.setAccessible(true);
            f.set(u2 , "李四");
            System.out.println(u2);
        }catch (Exception e1){
            e1.printStackTrace();
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值