getDeclaredFields,getFields,getDeclaredMethods,getMethods实例测试!
2015-06-03 23:52
375 查看
今日学习Class类中几个常用的方法getDeclaredFields,getFields,getDeclaredMethod,getMethod。
接下来来看看他们都有什么用
1、getFields
返回的也是一个Field数组,数组中是当前类,其父类和所实现的接口中的public成员变量。(只有public的偶~~~)
2、getDeclaredFields
简明扼要的说就是返回一个Field数组,数组中是当前类中的成员变量。(public、protected、private和default)
3、getMethods
返回一个Method数组,数组中包含当前类、其父类和实现接口中的public方法。(同getFields,也是只有public的)
4、getDeclaredMethod
返回一个Method数组,数组中包含当前类中所有类型的方法(public、protected、private和default),不包含父类及接口中的方法。
测试代码中有四个类
Human,Animal,Student和ClassApiTest
测试结果如下图:
That‘s all~~
接下来来看看他们都有什么用
1、getFields
返回的也是一个Field数组,数组中是当前类,其父类和所实现的接口中的public成员变量。(只有public的偶~~~)
2、getDeclaredFields
简明扼要的说就是返回一个Field数组,数组中是当前类中的成员变量。(public、protected、private和default)
3、getMethods
返回一个Method数组,数组中包含当前类、其父类和实现接口中的public方法。(同getFields,也是只有public的)
4、getDeclaredMethod
返回一个Method数组,数组中包含当前类中所有类型的方法(public、protected、private和default),不包含父类及接口中的方法。
测试代码中有四个类
Human,Animal,Student和ClassApiTest
//Human为父类 package reflect; public class Human { public String humanPublicVariable; protected String humanProtectedVariable; private String humanPrivateVariable; String humanDefaultVariable; public void humanPublicMethod(){ } protected void humanProtectedMethod() { } private void humanPrivateMethod(){ } void humanDefaultMethod(){ } } package reflect; /** * @author 作者 超 * @date 创建时间:2015年6月3日 下午11:42:11 * @version 1.0 * @description Animal为接口 */ public interface Animal { String animalInterfaceVariable = ""; void animalInterfaceMethod(); } package reflect; /** * @author 作者 超 * @date 创建时间:2015年6月3日 下午11:01:32 * @version 1.0 * @description Student 是子类 */ public class Student extends Human implements Animal{ public String studentPublicVariable; protected String studentProtectedVariable; private String studentPrivateVariable; String studentDefaultVariable; public void studentPublicMethod(){ } protected void studentProtectedMethod() { } private void studentPrivateMethod(){ } void studentDefaultMethod(){ } @Override public void animalInterfaceMethod() { } } package reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @author 作者 超 * @date 创建时间:2015年6月3日 下午11:07:11 * @version 1.0 * @description ClassApiTest类用于测试Class类中常用的api * getDeclaredFields,getFields */ public class ClassApiTest { public static void main(String[] args) { Student stu = new Student(); System.out.println("\n----------测试getDeclaredFields方法---------\n" + "****getDeclaredFields方法用于获取当前类中所有变量****"); Field[] f = stu.getClass().getDeclaredFields(); for (Field field : f) { System.out.println("field:"+field.toString()); } System.out.println("\n----------测试getFields方法---------\n" + "****getFields方法用于获取当前类、父类及实现接口中的!!public!!变量****"); Field[] f1 = stu.getClass().getFields(); for (Field field : f1) { System.out.println("field:"+field.toString()); } System.out.println("\n----------测试getDeclaredMethods方法---------\n" + "****getDeclaredMethods方法用于获取当前类中所有方法****"); Method[] m = stu.getClass().getDeclaredMethods(); for (Method method : m) { System.out.println("method:"+method.toString()); } System.out.println("\n----------测试getMethods方法---------\n" + "****getMethods方法用于获取当前类、父类及实现接口中的!!public!!方法****"); Method[] m1 = stu.getClass().getMethods(); for (Method method : m1) { System.out.println("method:"+method.toString()); } } }
测试结果如下图:
That‘s all~~
相关文章推荐
- Android Manifest 用法
- 一个小型js框架myJSFrame附API使用帮助
- 详细分析交换机、路由器、集线器的区别和联系
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载
- 揪出交换机端口背后“凶手”导致网速太慢
- 电脑重启后突然检测不到硬盘的原因分析与解决办法
- C#中设计、使用Fluent API
- Google官方支持的NodeJS访问API,提供后台登录授权
- PQ分区出错! 巧用Ghost急速补救的绝妙办法
- 深入C++中API的问题详解
- 使用WindowsAPI实现播放PCM音频的方法
- 40余种刻录盘片详细对照分析介绍第1/2页
- fullCalendar中文API官方文档
- 详解ASP.NET WEB API 之属性路由
- jquery与google map api结合使用 控件,监听器
- JavaScript中的Web worker多线程API研究
- FCKeditor提供了一个完整的JavaScript API