zoukankan      html  css  js  c++  java
  • java.lang.reflect操作对象属性(域)的值

    package reflect;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    /*2015-10-28*/
    public class RefactorDemo {
    
        /**
         * @param args
         * @throws NoSuchFieldException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws NoSuchMethodException
         * @throws InvocationTargetException
         */
        public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
            String name = "Demo";
            double salary = 20000.0;
            String nameFieldName = "name";
            String salaryFieldName = "salary";
    
            Constructor<?>[] constructors = Person.class.getConstructors();
            for (Constructor<?> constructor : constructors) {
                Class<?>[] parameterTypes = constructor.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    System.out.println(parameterType.getName());
                }
            }
    
            System.out.println("====================================");
            //constructor
            // 这个地方需要关注:如果是基本类型,就是double.class,不是Double.class
            Constructor<Person> constructor = Person.class.getConstructor(String.class, double.class);
            Person tom = constructor.newInstance("Tom", 3000000.0);
            System.out.println(tom);
    
    
            // get private Field
            Person person = new Person(name, salary);
            Field field = person.getClass().getDeclaredField(nameFieldName);
            field.setAccessible(true);// 访问控制
            System.out.println(field.get(person));
    
            person = new Person(name, salary);
            field = person.getClass().getDeclaredField(salaryFieldName);
            field.setAccessible(true);
            System.out.println(field.getDouble(person));
    
            // set private Field
            person = new Person();
            field = person.getClass().getDeclaredField(nameFieldName);
            field.setAccessible(true);
            field.set(person, name);
            field = person.getClass().getDeclaredField(salaryFieldName);
            field.setAccessible(true);
            field.setDouble(person, salary);
            System.out.println(person);
    
            // process method
            person = new Person();
            field = person.getClass().getDeclaredField(salaryFieldName);
            field.setAccessible(true);
            field.setDouble(person, 100000.9);
            Method method = person.getClass().getDeclaredMethod("doubleSalary");
            method.invoke(person);
            System.out.println(field.getDouble(person));
        }
    }
    
    class Person {
        private String name;
        private double salary;
    
        public Person() {
            super();
        }
    
        public Person(String name, double salary) {
            super();
            this.name = name;
            this.salary = salary;
        }
    
        public void doubleSalary() {
            this.salary = this.salary * 2;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", salary=" + salary + "]";
        }
    
    }

    Output:

    java.lang.String
    double
    ====================================
    Person [name=Tom, salary=3000000.0]
    Demo
    20000.0
    Person [name=Demo, salary=20000.0]
    200001.8
  • 相关阅读:
    编译原理 First集和Follow集的求法
    编译原理——算符优先分析法详解
    api.js(接口文件)
    addmul.wxs(保留两位小数-将手机号中间4位数变成*-处理时间戳)
    插槽的使用
    scroll-view小程序侧边栏(点击加载右侧商品)
    Array.of
    es6解构赋值默认值结合使用
    ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面。
    es6 数组的新方法 some filter indexOf 展开运算符... let const
  • 原文地址:https://www.cnblogs.com/softidea/p/4918870.html
Copyright © 2011-2022 走看看