A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xuemeng 中级黑马   /  2013-5-23 11:02  /  1359 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 xuemeng 于 2013-5-23 11:10 编辑

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//操作bean的指定属性
class Demo {
        public static void main(String[] args) throws ClassNotFoundException,
                        InstantiationException, IllegalAccessException,
                        IllegalArgumentException, InvocationTargetException,
                        NoSuchFieldException, SecurityException, IntrospectionException {
                test();
        }

        public static void test() throws ClassNotFoundException,
                        IntrospectionException, InstantiationException,
                        IllegalAccessException, IllegalArgumentException,
                        InvocationTargetException, NoSuchFieldException, SecurityException {
                Class c = Class.forName("cn.itcast.demo.Person");
                Object obj = c.newInstance();
                // 创建一个属性描述起对象, 并与指定bean属性的名称关联
                PropertyDescriptor pd = new PropertyDescriptor("age", c);

                // 获取指定bean属性的set方法
                Method m1 = pd.getWriteMethod();
                // 设置age属性的值为25
                m1.invoke(obj, 25);
                // 获取属性对象
                Field f = c.getField("age");
                // 暴力访问
                f.setAccessible(true);
                // 获取该属性对应的值
                int age = (int) f.get(obj);
                System.out.println(age);
        }
}

class Person {
        private String name;
        private int 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;
        }

        public String getA() {
                return null;
        }

晕, 原来方法用错了, 获取的是公共属性

3 个回复

倒序浏览
有错误提示要贴出来,一行一行找很慢的
回复 使用道具 举报
package com.luodi;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//操作bean的指定属性
class Demo {
        public static void main(String[] args) throws ClassNotFoundException,
                        InstantiationException, IllegalAccessException,
                        IllegalArgumentException, InvocationTargetException,
                        NoSuchFieldException, SecurityException, IntrospectionException {
                test();
        }

        public static void test() throws ClassNotFoundException,
                        IntrospectionException, InstantiationException,
                        IllegalAccessException, IllegalArgumentException,
                        InvocationTargetException, NoSuchFieldException, SecurityException {
                Class c = Class.forName("com.luodi.Person");
                Object obj = c.newInstance();
                // 创建一个属性描述起对象, 并与指定bean属性的名称关联
                PropertyDescriptor pd = new PropertyDescriptor("age", c);

                // 获取指定bean属性的set方法
                Method m1 = pd.getWriteMethod();
                // 设置age属性的值为25
                m1.invoke(obj, 25);
                // 获取属性对象
                Field f = c.getDeclaredField("age");
                // 暴力访问
                f.setAccessible(true);
                // 获取该属性对应的值
                int age =(Integer)f.get(obj);
                System.out.println(age);
        }
}

class Person {
        private String name;
        private int 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;
        }

        public String getA() {
                return null;
        }
}
回复 使用道具 举报
私有属性不能这么获取 c.getField()看我的那个方法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马