JavaWeb-1.2.2基础加强:反射之Class对象功能
Posted yub4by
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb-1.2.2基础加强:反射之Class对象功能相关的知识,希望对你有一定的参考价值。
1 package com.yubaby.reflect; 2 3 import com.yubaby.domain.Person; 4 5 import java.lang.reflect.Constructor; 6 import java.lang.reflect.Field; 7 import java.lang.reflect.Method; 8 9 /* 10 Class对象功能: 11 * getXxx()只能获取到public权限修饰的 12 * getDeclaredXxx()各种访问权限修饰的都能获取到 13 14 * 获取功能: 15 1. 获取成员变量 16 * Field[] getFields() 获取所有public修饰的成员变量 17 * Field getField(String name) 获取指定名称的 public修饰的成员变量 18 * Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符 19 * Field getDeclaredField(String name) 获取指定名称的成员变量,不考虑修饰符 20 21 * Field:成员变量 22 * 操作: 23 1. 设置值 24 * void set(Object obj, Object value) 25 2. 获取值 26 * get(Object obj) 27 3. 忽略访问权限修饰符的安全检查 28 * setAccessible(true):暴力反射 29 * 构造方法、成员方法也都支持暴力反射 30 31 2. 获取构造方法 32 * Constructor<?>[] getConstructors() 33 * Constructor<T> getConstructor(类<?>... parameterTypes) 34 * Constructor<T> getDeclaredConstructor(类<?>... parameterTypes) 35 * Constructor<?>[] getDeclaredConstructors() 36 37 * Constructor:构造方法 38 * 创建对象: 39 * T newInstance(Object... initargs) 40 * 如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法 41 42 3. 获取成员方法 43 * Method[] getMethods() 44 * Method getMethod(String name, 类<?>... parameterTypes) 45 * Method[] getDeclaredMethods() 46 * Method getDeclaredMethod(String name, 类<?>... parameterTypes) 47 48 * Method:方法对象 49 * 执行方法: 50 * Object invoke(Object obj, Object... args) 51 * 获取方法名称: 52 * String getName:获取方法名 53 54 4. 获取全类名 55 * String getName() 56 */ 57 public class ReflectDemo2 { 58 public static void main(String[] args) throws Exception { 59 // learnField(); 60 // learnConstructor(); 61 learnMethod(); 62 } 63 64 private static void learnField() throws Exception{ 65 Class<Person> personClass = Person.class; //获取Person类的Class对象 66 67 // 1.1 Field[] getFields() 68 Field[] fields = personClass.getFields(); 69 for (Field field: fields){ 70 System.out.println(field); 71 } 72 //public int com.yubaby.domain.Person.a 73 74 System.out.println("--------------------"); 75 76 //1.2 Field getField(String name) 77 Field a = personClass.getField("a"); 78 //获取成员变量a的值 79 Person person = new Person(); 80 Object value = a.get(person); 81 System.out.println(value); //int变量未赋值时,默认值为0 82 //设置a的值 83 a.set(person, 100); 84 System.out.println(person); //Person{name=\'null\', age=0, a=100, b=0, c=0, d=0} 85 86 System.out.println("--------------------"); 87 88 //1.3 Field[] getDeclaredFields() 89 Field[] declaredFields = personClass.getDeclaredFields(); 90 for (Field field: declaredFields){ 91 System.out.println(field); 92 } 93 /* 94 private java.lang.String com.yubaby.domain.Person.name 95 private int com.yubaby.domain.Person.age 96 public int com.yubaby.domain.Person.a 97 protected int com.yubaby.domain.Person.b 98 int com.yubaby.domain.Person.c 99 private int com.yubaby.domain.Person.d 100 */ 101 102 System.out.println("--------------------"); 103 104 //1.4 Field getDeclaredField(String name) 105 /*Field d = personClass.getDeclaredField("d"); 106 Object value2 = d.get(person); //报错,非法访问异常 107 System.out.println(value2);*/ 108 Field d = personClass.getDeclaredField("d"); 109 d.setAccessible(true); //暴力反射:忽略访问权限修饰符的安全检查 110 Object value2 = d.get(person); //0 111 System.out.println(value2); 112 } 113 114 private static void learnConstructor() throws Exception{ 115 Class<Person> personClass = Person.class; 116 117 //2.1 Constructor<?>[] getConstructors() 118 Constructor<Person> constructor = personClass.getConstructor(); 119 System.out.println(constructor); //public com.yubaby.domain.Person() 120 //创建对象 121 Person person = constructor.newInstance(); 122 System.out.println(person); //Person{name=\'null\', age=0, a=0, b=0, c=0, d=0} 123 //无参构造方法创建对象是的简化操作 124 // Person person2 = personClass.newInstance(); 125 // System.out.println(person2); ////Person{name=\'null\', age=0, a=0, b=0, c=0, d=0} 126 127 //2.2 Constructor<T> getConstructor(类<?>... parameterTypes) 128 Constructor<Person> constructor1 = personClass.getConstructor(String.class, int.class); 129 System.out.println(constructor1); //public com.yubaby.domain.Person(java.lang.String,int) 130 //创建对象 131 Person person1 = constructor1.newInstance("tom", 24); 132 System.out.println(person1); //Person{name=\'tom\', age=24, a=0, b=0, c=0, d=0} 133 134 //2.3 Constructor<T> getDeclaredConstructor(类<?>... parameterTypes) 135 //2.4 Constructor<?>[] getDeclaredConstructors() 136 } 137 138 private static void learnMethod() throws Exception{ 139 Class<Person> personClass = Person.class; 140 141 //3.1 Method[] getMethods() 142 Method eatMethod = personClass.getMethod("eat"); 143 Person person = new Person(); 144 person.eat(); //eat... 145 eatMethod.invoke(person); //eat... 146 147 //获取方法名 148 System.out.println(eatMethod.getName()); //eat 149 //获取类名 150 System.out.println(personClass.getName()); //com.yubaby.domain.Person <-- 全类名:含有包名 151 152 Method eatMethod2 = personClass.getMethod("eat", String.class); 153 person.eat("米饭"); //eat:米饭 154 eatMethod2.invoke(person, "面条"); //eat:面条 155 System.out.println(eatMethod.getName()); //eat 156 157 //3.2 Method getMethod(String name, 类<?>... parameterTypes) 158 Method[] methods = personClass.getMethods(); 159 for (Method method: methods){ 160 System.out.println(method); 161 } 162 /* 163 包含有父类Object类的方法 164 public java.lang.String com.yubaby.domain.Person.getName() 165 public java.lang.String com.yubaby.domain.Person.toString() 166 public void com.yubaby.domain.Person.setName(java.lang.String) 167 public void com.yubaby.domain.Person.setAge(int) 168 public int com.yubaby.domain.Person.getAge() 169 public void com.yubaby.domain.Person.eat() 170 public void com.yubaby.domain.Person.eat(java.lang.String) 171 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException 172 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException 173 public final void java.lang.Object.wait() throws java.lang.InterruptedException 174 public boolean java.lang.Object.equals(java.lang.Object) 175 public native int java.lang.Object.hashCode() 176 public final native java.lang.Class java.lang.Object.getClass() 177 public final native void java.lang.Object.notify() 178 public final native void java.lang.Object.notifyAll() 179 */ 180 181 //3.3 Method[] getDeclaredMethods() 182 //3.4 Method getDeclaredMethod(String name, 类<?>... parameterTypes) 183 } 184 }
1 package com.yubaby.domain; 2 3 public class Person { 4 private String name; 5 private int age; 6 7 public int a; 8 protected int b; 9 int c; 10 private int d; 11 12 public Person() { 13 } 14 15 public Person(String name, int age) { 16 this.name = name; 17 this.age = age; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public void setAge(int age) { 25 this.age = age; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 @Override 37 public String toString() { 38 return "Person{" + 39 "name=\'" + name + \'\\\'\' + 40 ", age=" + age + 41 ", a=" + a + 42 ", b=" + b + 43 ", c=" + c + 44 ", d=" + d + 45 \'}\'; 46 } 47 48 public void eat(){ 49 System.out.println("eat..."); 50 } 51 52 public void eat(String food){ //函数重载 53 System.out.println("eat:" + food); 54 } 55 }
以上是关于JavaWeb-1.2.2基础加强:反射之Class对象功能的主要内容,如果未能解决你的问题,请参考以下文章
JavaWeb1.3.3基础加强:注解案例(注解+反射重构"JavaWeb-1.2.3基础加强:案例(反射+配置文件))
阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_5_反射_概述
阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_10_反射_Class对象功能_获取Method成员方法