代码测试:java反射中getXXX和getDeclaredXXX的区别
来源:互联网 发布:js数组 key push 编辑:程序博客网 时间:2024/04/30 07:38
原创不易,转载请注明出处,水平有限,有错漏敬请指出!
直接说结论,大伙有时间再看代码
一、结论:
1.用getXXX方法,只能取到自己和父类的public的属性或方法,其他都取不到。
2.用getDeclaredXXX方法,类里面定义的所有字段或方法,不论是什么修饰符,都能取到。
但不能取父类的任何字段。
二、注意:
1.Java是可以多级继承的,能取父类的public属性或方法,其实是能取它所有祖宗的public属性或方法的意思.
2.Java所有的类都继承了Object类,
所以,只要用了getXXX,就一定能取到Object类的public的方法.(Object类没有属性)
3.getDeclaredXXX能取到重写父类的方法
4.对于private的属性,有没有提供get,set方法跟用反射取值没关系,取不到就是取不到,提供了也没用。
三、代码思路
定义了一个Human类和Boy类,Boy类继承Human类,两个类都定义了用public,protect,default,private修饰
的方法和属性各1个,并各定义了1个XXXPrivateFieldWithGetSet属性,提供get,set方法。Human类重写了
Object类的toString()方法。然后打印通过getXXX和getDeclaredXXX取到的值,比较得出结论。
四、测试的源码
4.1 Human类
package com.cherrycheng.study.reflect.bean;/** * 人类 * 2016年3月18日 * @author chengru */public class Human {/** Human类的public属性*/public String humanPublicField;/** Human类的protected属性*/protected int humanProtectedField;/** Human类的default属性*/String humanDefaultField;/** Human类的private属性,提供getter,setter方法*/private String humanPrivateFieldWithGetSet;/** Human类的private属性,无getter,setter方法*/private String humanPrivateFieldNoGetSet;//----------------------- Override methods------------------------@Overridepublic String toString() {return "";};//----------------------- user-defined methods------------------------public void humanPublicMethod(){System.out.println("Human类的public方法");}protected void humanProtectedMethod(){System.out.println("Human类的protected方法");}void humanDefaultMethod(){System.out.println("Human类的default方法");}private void humanPrivateMethod(){System.out.println("Human类的private方法");}//-----------------------gets and sets------------------------public String getHumanPrivateFieldWithGetSet() {return humanPrivateFieldWithGetSet;}public void setHumanPrivateFieldWithGetSet(String humanPrivateFieldWithGetSet) {this.humanPrivateFieldWithGetSet = humanPrivateFieldWithGetSet;}}
4.2 Boy类
package com.cherrycheng.study.reflect.bean;/** * 男孩是人类的子类,继承人类的属性 * 2016年3月18日 * @author chengru */public class Boy extends Human{/** Boy类的public属性*/public String boyPublicField;/** Boy类的protected属性*/protected int boyProtectedField;/** Boy类的default属性*/String boyDefaultField;/** Boy类的private属性,提供getter,setter方法*/private String boyPrivateFieldWithGetSet;/** Boy类的private属性,无getter,setter方法*/private String boyPrivateFieldNoGetSet;//----------------------- user-defined methods------------------------public void boyPublicMethod(){System.out.println("Boy类的public方法");}protected void boyProtectedMethod(){System.out.println("Boy类的protected方法");}void boyDefaultMethod(){System.out.println("Boy类的default方法");}private void boyPrivateMethod(){System.out.println("Boy类的private方法");}//-----------------------getters and setters------------------------public String getBoyPrivateFieldWithGetSet() {return boyPrivateFieldWithGetSet;}public void setBoyPrivateFieldWithGetSet(String boyPrivateFieldWithGetSet) {this.boyPrivateFieldWithGetSet = boyPrivateFieldWithGetSet;}}
4.3 测试类
package com.cherrycheng.study.reflect.test;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.junit.Test;import com.cherrycheng.study.reflect.bean.Boy;import com.cherrycheng.study.reflect.bean.Human;/** * 测试有继承和没继承的类使用反射的getXXX和getDeclaredXXX的区别<br/> * 2016年3月18日 * @author chengru */public class DeclaredAndNot {private void printFields(Class<?> cl){int i=1;System.out.println("------------"+cl.getName()+"'s Fields-----------");for(Field f:cl.getFields()){System.out.print(i+"、["+f.getName()+"] \t ");++i;}System.out.println();int j=1;System.out.println("------------"+cl.getName()+"'s DeclaredFields-----------");for(Field f:cl.getDeclaredFields()){System.out.print(j+"、["+f.getName()+"] \t ");++j;}System.out.println();System.out.println();}private void printMethods(Class<?> cl){int i=1;System.out.println("------------"+cl.getName()+"'s Methods-----------");for(Method f:cl.getMethods()){System.out.print(i+"、["+f.getName()+"] \t ");++i;}System.out.println();int j=1;System.out.println("------------"+cl.getName()+"'s DeclaredMethods-----------");for(Method f:cl.getDeclaredMethods()){System.out.print(j+"、["+f.getName()+"] \t ");++j;}System.out.println();System.out.println();}/** * 测试继承的反射 * 2016年3月18日 * @author chengru */@Testpublic void TestInheritReflect(){printFields(Boy.class);printMethods(Boy.class);}/** * 测试对自身的反射 * 2016年3月18日 * @author chengru */@Testpublic void TestSelfReflect(){printFields(Human.class);printMethods(Human.class);}}
五 运行结果
------------com.cherrycheng.study.reflect.bean.Boy's Fields-----------1、[boyPublicField] 2、[humanPublicField] ------------com.cherrycheng.study.reflect.bean.Boy's DeclaredFields-----------1、[boyPublicField] 2、[boyProtectedField] 3、[boyDefaultField] 4、[boyPrivateFieldWithGetSet] 5、[boyPrivateFieldNoGetSet]
------------com.cherrycheng.study.reflect.bean.Boy's Methods-----------1、[boyPublicMethod] 2、[getBoyPrivateFieldWithGetSet] 3、[setBoyPrivateFieldWithGetSet] 4、[toString] 5、[humanPublicMethod] 6、[setHumanPrivateFieldWithGetSet] 7、[getHumanPrivateFieldWithGetSet] 8、[wait] 9、[wait] 10、[wait] 11、[equals] 12、[hashCode] 13、[getClass] 14、[notify] 15、[notifyAll] ------------com.cherrycheng.study.reflect.bean.Boy's DeclaredMethods-----------1、[boyPublicMethod] 2、[boyProtectedMethod] 3、[boyDefaultMethod] 4、[boyPrivateMethod] 5、[getBoyPrivateFieldWithGetSet] 6、[setBoyPrivateFieldWithGetSet]
------------com.cherrycheng.study.reflect.bean.Human's Fields-----------1、[humanPublicField] ------------com.cherrycheng.study.reflect.bean.Human's DeclaredFields-----------1、[humanPublicField] 2、[humanProtectedField] 3、[humanDefaultField] 4、[humanPrivateFieldWithGetSet] 5、[humanPrivateFieldNoGetSet]
------------com.cherrycheng.study.reflect.bean.Human's Methods-----------1、[toString] 2、[humanPublicMethod] 3、[setHumanPrivateFieldWithGetSet] 4、[getHumanPrivateFieldWithGetSet] 5、[wait] 6、[wait] 7、[wait] 8、[equals] 9、[hashCode] 10、[getClass] 11、[notify] 12、[notifyAll] ------------com.cherrycheng.study.reflect.bean.Human's DeclaredMethods-----------1、[toString] 2、[humanPublicMethod] 3、[humanProtectedMethod] 4、[humanDefaultMethod] 5、[humanPrivateMethod] 6、[setHumanPrivateFieldWithGetSet] 7、[getHumanPrivateFieldWithGetSet]
0 0
- 代码测试:java反射中getXXX和getDeclaredXXX的区别
- Java反射中getXXX和getDeclaredXXX的解析
- Java中Class.getXXX()和Class.getDeclaredXXX()的区别
- 安卓中使用反射以及getDeclaredXXX()和getXXX()方法的区别
- Java的Class类提供了许多的getDeclaredxxx和getxxx方法,例如getDeclaredMethod和getMethod成对出现,那它们有什么区别呢?
- java中new和反射的区别
- JSONObject对象的optXXX和getXXX的区别
- java反射中,Class.forName和classloader的区别(代码说话)
- Struts2中使用execAndWait后,在 Action中调用getXXX()方法报告java.lang.NullPointerException异常的原因和解决方法
- java反射中getDeclaredMethods和getMethods的区别
- java反射中getDeclaredField和getField的区别
- java反射中getDeclaredMethods和getMethods的区别
- struts2的debug标签和getXxx方法
- 【备忘】java中File类的getXxx(路径相关方法)返回值
- 反射中getClass()和class的区别
- Java反射 getFields()和getDeclaredFields()的区别
- Java反射 getFields()和getDeclaredFields()的区别
- Java反射 getFields()和getDeclaredFields()的区别
- SSM框架——使用MyBatis Generator自动创建代码
- HTML、CSS、JavaScript学习总结
- HDU1056 Tree Cutting(最小点覆盖)
- 关于成为架构师的几个思考?
- HDU 2203 亲和串
- 代码测试:java反射中getXXX和getDeclaredXXX的区别
- 计算流图中的前必经节点
- fuel 5.1在vmware esxi上的部署
- 基于密度的聚类算法(DBSCAN)的java实现
- iOS设置tabbar的显示之 imageWithRenderingMode:
- Find the Spy
- (8)Python爬虫——爬取豆瓣影评数据
- 蓝桥杯 历届试题 奇怪的比赛
- 深入理解Intent和IntentFilter