(转)Field getField(String name)与Field getDeclaredField(String name)的区别

Field getField(String name)返回已加载类声明的所有public成员变量的Field对象,包括从父类继承过来的成员变量,参数name指定成员变量的名称

而Field getDeclaredField(String name)不能获取从父类那继承过来的成员变量

***********************************************

 
  
1 package cn.com.vo;
2
3 public class Father {
4 public String name = " 父亲 " ;
5
6 public String sex = " " ;
7
8 public String getName() {
9 return name;
10 }
11
12 public void setName(String name) {
13 this .name = name;
14 }
15
16 public String getSex() {
17 return sex;
18 }
19
20 public void setSex(String sex) {
21 this .sex = sex;
22 }
23
24
25 }
26 +++++++++++++++++++++++++++++++++
27
28 package cn.com.vo;
29
30 public class Son extends Father{
31
32 // public String name;
33 public int age = 20 ;
34
35
36 }
37 =========================================
38
39 package cn.com.vo;
40
41 import java.lang.reflect.Constructor;
42 import java.lang.reflect.Field;
43 import java.lang.reflect.InvocationTargetException;
44 import java.lang.reflect.Method;
45
46 import cn.com.vo.UserVo;
47
48 public class TestReflect {
49
50 /**
51 * java反射机制
52 */
53 public static void main(String[] args) {
54 try {
55 // 类的动态加载
56 Class c = Class.forName( " cn.com.vo.Son " ); // 方式一
57 Son vo = (Son)c.newInstance();
58
59 Field f = c.getField( " name " );
60 System.out.println(f.get(vo));
61
62
63 // 注意:下段代码会报错,
64 /* Field f2=c.getDeclaredField("name");
65 System.out.println(f2.get(vo)); */
66
67 } catch (ClassNotFoundException e) {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 } catch (SecurityException e) {
71 // TODO Auto-generated catch block
72 e.printStackTrace();
73 } catch (NoSuchFieldException e) {
74 // TODO Auto-generated catch block
75 e.printStackTrace();
76 } catch (InstantiationException e) {
77 // TODO Auto-generated catch block
78 e.printStackTrace();
79 } catch (IllegalAccessException e) {
80 // TODO Auto-generated catch block
81 e.printStackTrace();
82 }
83 }
84
85 }

转载于:https://www.cnblogs.com/keis/archive/2011/05/10/2042408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮我优化下面的java代码for (Object object : args) { if (object == null) { continue; } if(!dataPermissionSchemaEoMap.containsKey(databaseName+tableName)){ continue; } if (object instanceof BaseEo) { Class<?> aClass = object.getClass(); Object o = aClass.newInstance(); BeanUtils.copyProperties(object,o); //获取字段 Field[] fieldsAndParentsFields = ReflectionUtil.getFieldsAndParentsFields(o); //获取对应的行权限 DataPermissionSchemaEo dataPermissionSchemaEo = dataPermissionSchemaEoMap.get(databaseName + tableName); List<SchemaRowRuleEo> schemaRowRuleList = dataPermissionSchemaEo.getSchemaRowRuleList(); for (SchemaRowRuleEo schemaRowRuleEo : schemaRowRuleList) { //获取行规则要素 String filterConditionList = schemaRowRuleEo.getFilterConditionList(); List<SchemaRowFilterCondition> schemaRowFilterConditions = JSONObject.parseArray(filterConditionList, SchemaRowFilterCondition.class); for (SchemaRowFilterCondition schemaRowFilterCondition : schemaRowFilterConditions) { String field = schemaRowFilterCondition.getField(); String camel = underlineToCamel(field); String key = schemaRowFilterCondition.getKey(); //如果字段相等 for (Field fieldsAndParentsField : fieldsAndParentsFields) { if(StringUtils.equals(fieldsAndParentsField.getName(),camel) ){ Field f = aClass.getDeclaredField(camel); f.setAccessible(true); Object value = f.get(object); if(StringUtils.equals(key,value.toString()) && schemaRowRuleEo.getRowEditPermission() ){ System.out.println("修改成功"); }else { throw new CustomException("大马猴来了",code); } } } } } } }代码优化
05-30

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值