笔记: Field 与 Method
来源:互联网 发布:ubuntu 16.04 新特性 编辑:程序博客网 时间:2024/06/06 01:33
java.lang.reflect.FieldAPI解释:Field 提供有关类或接口的单个字段的信息,以及对它的动态访问权限。反射的字段可能是一个类(静态)字段或实例字段。它的使用是从反射开始的,通常是通过Class对象来获取Field实例:Class.getFields(), Class.getField(String), Class.getDeclaredFields(), Class.getDeclaredField(String)方法说明: Field getDeclaredField(String name) 返回具有给定名称的字段的Field对象,该对象在此Class表示的类中声明,注意此返回对象只是声明, 要获取对应值需要调用 field.get(对象),包括private Field[] getDeclaredFields() 返回一个数组,该数组包含由此类表示的类中声明的所有字段的Field对象,包括private Field getField(String name) 返回指定name的属性值,此属性值必须是public范围 Field[] getFields() 返回该类中声明的所有字段的Field对象(仅public)关于Field的常用方法:boolean equals(Object obj) 将此 Field 与指定对象比较Object get(Object obj) 返回指定对象上此 Field 表示的字段的值 <T extends Annotation> getAnnotation(Class<T> annotationClass) 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 nullboolean getBoolean(Object obj) 获取一个静态或实例 boolean 字段的值//相似方法 //getByte(Object obj) //getChar(Object obj) //getDouble(Object obj) //getFloat(Object obj) //getInt(Object obj) //getLong(Object obj) //getShort(Object obj) Annotation[] getDeclaredAnnotations() 返回直接存在于此元素上的所有注释int getModifiers() 以整数形式返回由此 Field 对象表示的字段的 Java 语言修饰符String getName() 返回此 Field 对象表示的字段的名称boolean isEnumConstant() 如果此字段表示枚举类型的元素,则返回 true;否则返回 falsevoid set(Object obj, Object value) 将指定对象变量上此 Field 对象表示的字段设置为指定的新值//相似的方法//setByte(Object obj, byte b) //setChar(Object obj, char c) //setDouble(Object obj, double d) //setFloat(Object obj, float f)//setInt(Object obj, int i) //setLong(Object obj, long l) //setShort(Object obj, short s) String toGenericString() 返回一个描述此 Field(包括其一般类型)的字符串 eg:public boolean UserTest.sex还有从类 java.lang.reflect.AccessibleObject 继承的几个方法:getAnnotations //返回当前这个元素上的所有注释isAccessible //值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查isAnnotationPresent //用来判断指定注释类型是存在于此元素上setAccessible //当Field是private的,要先setAccessible(true)java.lang.reflect.MethodAPI解释: Method 提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。所反映的方法可能是类方法或实例方法(包括抽象方法)。 Method 允许在匹配要调用的实参与底层方法的形参时进行扩展转换;但如果要进行收缩转换,则会抛出 IllegalArgumentException。 它的使用是从反射开始的,通常是通过Class对象来获取Field实例: Class.getMethods(), Class.getMethod(String, Class[]), Class.getDeclaredMethods(), Class.getDeclaredMethod(String, Class[])方法说明:Method getDeclaredMethod(String name, Class...<?> parameterTypes)返回一个Method对象,该对象表示与由此类表示的类声明的指定名称和参数类型匹配的方法。Method[] getDeclaredMethods() 返回一个数组,其中包含Method对象,用于在此类中表示的类中声明的所有方法。Method getEnclosingMethod() 如果是匿名或本地/自动类,则返回此类的封闭方法,否则为空。Method getMethod(String name, Class...<?> parameterTypes) 返回一个Method对象,该对象用指定的名称和参数类型表示公共方法。Method[] getMethods() 返回一个数组,该数组包含此Class所表示的类C的所有公共方法的Method对象。关于Method的常用方法:boolean equals(Object obj) 将此 Method 与指定对象进行比较 <T extends Annotation> getAnnotation(Class<T> annotationClass) 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 nullAnnotation[] getDeclaredAnnotations() 返回直接存在于此元素上的所有注释Class<?> getDeclaringClass() 返回表示声明由此 Method 对象表示的方法的类或接口的 Class 对象Object getDefaultValue() 返回由此 Method 实例表示的注释成员的默认值Type getGenericReturnType() 返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象,其实就是返回值类型int getModifiers() 以整数形式返回此 Method 对象所表示方法的 Java 语言修饰符,没有什么用String getName() 以 String 形式返回此 Method 对象表示的方法名称Annotation[][] getParameterAnnotations() 返回表示按照声明顺序对此 Method 对象所表示方法的形参进行注释的那个数组的数组Class<?>[] getParameterTypes() 按照声明顺序返回 Class 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型 Class<?> getReturnType() 返回一个 Class 对象,该对象描述了此 Method 对象所表示的方法的正式返回类型int hashCode() 返回此 Method 的哈希码Object invoke(Object obj, Object... args) 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法, 参数: obj - 从中调用底层方法的对象 args - 用于方法调用的参数 boolean isVarArgs() 如果将此方法声明为带有可变数量的参数,则返回 true;否则,返回 falseString toGenericString() 返回描述此 Method 的字符串,包括类型参数。另外Method也有从类 java.lang.reflect.AccessibleObject 继承的方法: getAnnotations, isAccessible, isAnnotationPresent, setAccessible
下来打印一下上面的一些方法:
UserTest类如下:
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@interface MethodTest { //注解String message();}public class UserTest {@Utils(message = "年龄不能小于0")public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}private int age;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean sex;public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}public String sayHello(String str){System.out.println(str);return str;}@MethodTest(message = "方法名为:privTest")private void privTest(){System.out.println("privTest");}}main方法中的内容:
public static void main(String[] args) { UserTest mUserTest = new UserTest();mUserTest.setAge(10);mUserTest.setName("zhangsan");Class<?> clazzClass = mUserTest.getClass(); try {Field[] files = clazzClass.getFields(); //返回该类中声明的所有字段的Field对象(仅public)//Field field = clazzClass.getField("age"); //返回该类中age对应的属性(仅public)Field[] files2 = clazzClass.getDeclaredFields(); //返回该类中声明的所有字段的Field对象(包括private)Field field2 = clazzClass.getDeclaredField("age");////返回该类中age对应的属性(包括private)field2.setAccessible(true);Field field_sex = clazzClass.getDeclaredField("sex");//获取一个静态或实例 boolean 字段的值(不用去判断类型,然后强转),相似用法//getByte(Object obj) //getChar(Object obj) //getDouble(Object obj) //getFloat(Object obj) //getInt(Object obj) //getLong(Object obj) //getShort(Object obj) System.out.println("field_sex: "+field_sex.getBoolean(mUserTest));for (Field subField : files2) {subField.setAccessible(true);//返回一个 Class 对象,它标识了此 Field 对象所表示字段的声明类型System.out.println("getType: "+subField.getType());//返回一个 Type 对象,它表示此 Field 对象所表示字段的声明类型System.out.println("getGenericType: "+subField.getGenericType());System.out.println("name: "+subField.getName());//返回此 Field 对象表示的字段的名称System.out.println("type_int: "+subField.getModifiers()); //返回指定对象上此 Field 表示的字段的值if(subField.getType().equals(String.class)){ //将此 Field 与指定对象比较String pass = (String)subField.get(mUserTest);//返回指定对象上此 Field 表示的字段的值System.out.println("fields_String: "+pass);}else if(subField.getType().equals(int.class)){ //将此 Field 与指定对象比较int age = (int)subField.get(mUserTest);System.out.println("fields_Integer: "+age);}else if(subField.getType().equals(boolean.class)){boolean sex = (boolean)subField.get(mUserTest);System.out.println("fields_Boolean: "+sex);}//如果存在该元素的指定类型的注释,则返回这些注释,否则返回 nullUtils annotation = subField.getAnnotation(Utils.class);if(annotation !=null){System.out.println(annotation.message()); //获取注解信息}System.out.println("---------------------------------------------------------");}//将指定对象变量上此 Field 对象表示的字段设置为指定的新值,即 将mUserTest对象上的field_sex属性值修改为true//field_sex.set(mUserTest, true); field_sex.setBoolean(mUserTest, true); //将字段的值设置为指定对象上的一个 boolean 值//相似的方法//setByte(Object obj, byte b) //setChar(Object obj, char c) //setDouble(Object obj, double d) //setFloat(Object obj, float f)//setInt(Object obj, int i) //setLong(Object obj, long l) //setShort(Object obj, short s) System.out.println("reset_field_sex: "+field_sex.getBoolean(mUserTest));//返回一个描述此 Field(包括其一般类型)的字符串System.out.println("toGenericString: "+field_sex.toGenericString());} catch (Exception e) {e.printStackTrace();} System.out.println("---------------------Method--------------------------------"); try {Method[] methods = clazzClass.getMethods(); //该数组包含此Class所表示的类的所有公共方法的Method对象(仅public)//返回一个Method对象,该对象用指定的名称和参数类型表示公共方法(仅public)//参数:第一个参数是具体调用该方法的对象 第二个参数是执行该方法的具体参数Method method = clazzClass.getMethod("sayHello",String.class); //返回一个数组,其中包含Method对象,用于在此类中表示的类中声明的所有方法。Method[] methods_all = clazzClass.getDeclaredMethods(); //返回一个Method对象,该对象表示与由此类表示的类声明的指定名称和参数类型匹配的方法,包括privateMethod method_priv = clazzClass.getDeclaredMethod("privTest",null);for (Method method2 : methods_all) {//返回直接存在于此元素上的所有注释Annotation[] mAnnotation =method2.getAnnotations();//如果存在该元素的指定类型的注释,则返回这些注释,否则返回 nullMethodTest methodTest = method2.getAnnotation(MethodTest.class); if(methodTest != null){System.out.println("methodTest_message: "+methodTest.message());}//返回表示声明由此 Method 对象表示的方法的类或接口的 Class 对象,其实就是类名Class<?> clazzClass2 = method2.getDeclaringClass();System.out.println("getDeclaringClass: " + clazzClass2.toString());//getDefaultValue 返回由此 Method 实例表示的注释成员的默认值System.out.println("getDefaultValue: "+method2.getDefaultValue());//按照声明顺序返回 Type 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型的Type[] type = method2.getGenericParameterTypes();for (Type type2 : type) {System.out.println("getGenericParameterTypes: "+type2.toString());}// getGenericReturnType 返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象,其实就是返回值类型System.out.println("getGenericReturnType: "+method2.getGenericReturnType());// getModifiers 以整数形式返回此 Method 对象所表示方法的 Java 语言修饰符System.out.println("getModifiers: "+method2.getModifiers());//getName() 以 String 形式返回此 Method 对象表示的方法名称System.out.println("getName: "+method2.getName());//getParameterTypes 按照声明顺序返回 Class 对象的数组,这些对象描述了此 Method 对象所表示的方法的形参类型Class<?>[] clazzClasses = method2.getParameterTypes();for (Class<?> class1 : clazzClasses) {System.out.println("getParameterTypes: "+class1.toString());}//返回一个 Class 对象,该对象描述了此 Method 对象所表示的方法的正式返回类型Class<?> clazzClasses2 = method2.getReturnType();System.out.println("getReturnType: "+clazzClasses2.toString());//对带有指定参数的指定对象调用由此 Method 对象表示的底层方法 //参数: obj - 从中调用底层方法的对象 args - 用于方法调用的参数method.invoke(mUserTest, "invoke");//toGenericString 返回描述此 Method 的字符串,包括类型参数。toString 返回描述此 Method 的字符串System.out.println("toGenericString: "+method2.toGenericString()+" toString"+method2.toString());System.out.println("------------------------------------");}} catch (Exception e) {e.printStackTrace();}}打印结果:field_sex: falsegetType: class java.lang.StringgetGenericType: class java.lang.Stringname: nametype_int: 1fields_String: zhangsan年龄不能小于0---------------------------------------------------------getType: intgetGenericType: intname: agetype_int: 2fields_Integer: 10---------------------------------------------------------getType: booleangetGenericType: booleanname: sextype_int: 1fields_Boolean: false---------------------------------------------------------reset_field_sex: truetoGenericString: public boolean UserTest.sex---------------------Method--------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericReturnType: class java.lang.StringgetModifiers: 1getName: getNamegetReturnType: class java.lang.StringinvoketoGenericString: public java.lang.String UserTest.getName() toStringpublic java.lang.String UserTest.getName()------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericParameterTypes: class java.lang.StringgetGenericReturnType: voidgetModifiers: 1getName: setNamegetParameterTypes: class java.lang.StringgetReturnType: voidinvoketoGenericString: public void UserTest.setName(java.lang.String) toStringpublic void UserTest.setName(java.lang.String)------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericParameterTypes: intgetGenericReturnType: voidgetModifiers: 1getName: setAgegetParameterTypes: intgetReturnType: voidinvoketoGenericString: public void UserTest.setAge(int) toStringpublic void UserTest.setAge(int)------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericParameterTypes: class java.lang.StringgetGenericReturnType: class java.lang.StringgetModifiers: 1getName: sayHellogetParameterTypes: class java.lang.StringgetReturnType: class java.lang.StringinvoketoGenericString: public java.lang.String UserTest.sayHello(java.lang.String) toStringpublic java.lang.String UserTest.sayHello(java.lang.String)------------------------------------methodTest_message: 方法名为:privTestgetDeclaringClass: class UserTestgetDefaultValue: nullgetGenericReturnType: voidgetModifiers: 2getName: privTestgetReturnType: voidinvoketoGenericString: private void UserTest.privTest() toStringprivate void UserTest.privTest()------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericParameterTypes: booleangetGenericReturnType: voidgetModifiers: 1getName: setSexgetParameterTypes: booleangetReturnType: voidinvoketoGenericString: public void UserTest.setSex(boolean) toStringpublic void UserTest.setSex(boolean)------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericReturnType: intgetModifiers: 1getName: getAgegetReturnType: intinvoketoGenericString: public int UserTest.getAge() toStringpublic int UserTest.getAge()------------------------------------getDeclaringClass: class UserTestgetDefaultValue: nullgetGenericReturnType: booleangetModifiers: 1getName: isSexgetReturnType: booleaninvoketoGenericString: public boolean UserTest.isSex() toStringpublic boolean UserTest.isSex()------------------------------------
2 0
- 笔记: Field 与 Method
- JAVA菜鸟入门(15) static method/ field与normal method/field的相互调用关系
- 反射与Constructor、Method、Field类的使用
- class&constructor&method&field
- Name、SuperClass、Constructor、Field、Method
- android jni ——Field & Method --> Accessing Field
- what is a synthetic field and method
- Java_反射_Class类(Method,Field,Constructor)
- Java_反射_Class类(Method,Field,Constructor)
- 字段field 属性 property 方法 method
- static field and method in java
- 黑马程序员------反射技术Constructor,Field,Method
- class中的field,property,method,constructor
- 反射获取Field和Method对象
- ART运行时之method/field加载
- 07_JNI Field Method Constructor_method Exception Memory
- Java反射机制--Class,Field,Method,Constructor
- JavaSE_Java反射机制详解(Field,Method) 及 Method.invoke使用方法
- JSON对象、中文乱码及时间转换-默然说话20170331备课笔记
- 用 Kotlin 开发 Android 项目是一种什么样的感受?
- Delphi XE10 使用百度定位SDK Jar包进行定位
- PAT 1131. Subway Map (30)
- 关于spinner水平偏移问题解决
- 笔记: Field 与 Method
- oracle 练习 之关联查询 练习
- 【web开发】:JSP连接Mysql数据库(非常详细,代码一看就懂)
- LeetCode 204 Count Primes
- Java sdut acm 3338 计算各种图形的周长(接口与多态)
- start with connect by prior 递归查询用法
- 鸟哥的服务器《六》SELinux管理原则
- c 程序设计语言 第二版 练习题 4-10
- JSP基本部署