使用反射技术操作类中的属性、对象及方法
package domain;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class Person {
private String name;
private int age;
//添加几个属性,按照作用域由大到小
public String a;
protected String b;
String c;
private String d;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
public void eat(){
System.out.println("eat====");
}
public void eat(String food){
System.out.println("eat===="+food);
}
private void drink(){
System.out.println("drink====");
}
private void drink(String water){
System.out.println("drink===="+water);
}
}
package domain;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class PersonPrivate {
private String name;
private int age;
//添加几个属性,按照作用域由大到小
public String a;
protected String b;
String c;
private String d;
private PersonPrivate(String name, int age) {
this.name = name;
this.age = age;
}
private PersonPrivate() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
}
package test;
import domain.Person;
import java.lang.reflect.Field;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class ReflectDemo2 {
public static void main(String[] args) throws Exception {
/**
Class对象功能:
* 获取功能:
1. 获取成员变量们
* Field[] getFields() //获取所有public修饰的成员变量
* Field getField(String name) //获取指定名称修饰的public成员变量
* Field[] getDeclaredFields() //获取所有的成员变量,不考虑修饰符
* Field getDeclaredField(String name) //获取指定的成员变量,不考虑修饰符
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
3. 获取成员方法们:
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
4. 获取类名
* String getName()
Field:成员变量
*操作:
*1.设置值:void set(Object obj,Object value)
*2.获取值:get(Object obj)
*3.忽略访问权限修饰符的安全检查 setAccessible(true):暴力反射
*/
//首先,获取Person的Class对象
//只能获取所有public修饰的成员变量
Class<Person> personClass = Person.class;
Field[] fields = personClass.getFields();
System.out.println("----------------------1");
for (Field field : fields) {
System.out.println(field);
}
System.out.println("----------------------2");
//2.Field getField(String name)
Field a = personClass.getField("a");
//获取成员变量a的值 属性a是用public修饰的
Person person = new Person();
Object value = a.get(person);
System.out.println(value);
//设置a属性的值
a.set(person,"丫丫");
System.out.println(person);
System.out.println("----------------------3");
//Field[] getDeclaredFields():获取所有的成员变量,不考虑修饰符
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
System.out.println("----------------------4");
//Field getDeclaredField(String name)
Field d = personClass.getDeclaredField("d");//d是用private修饰的
//忽略访问权限修饰符的安全检查
d.setAccessible(true);//也称之为;暴力反射 如果不加这行代码的话,是会报错的
Object o = d.get(person);
d.set(person,"李四");
System.out.println(person);
System.out.println(o);
}
}
运行结果:
----------------------1
public java.lang.String domain.Person.a
----------------------2
null
Person{name='null', age=0, a='丫丫', b='null', c='null', d='null'}
----------------------3
private java.lang.String domain.Person.name
private int domain.Person.age
public java.lang.String domain.Person.a
protected java.lang.String domain.Person.b
java.lang.String domain.Person.c
private java.lang.String domain.Person.d
----------------------4
Person{name='null', age=0, a='丫丫', b='null', c='null', d='李四'}
null
package test;
import domain.Person;
import domain.PersonPrivate;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class ReflectDemo3 {
public static void main(String[] args) throws Exception {
/**
Class对象功能:
* 获取功能:
1. 获取成员变量们
* Field[] getFields()
* Field getField(String name)
* Field[] getDeclaredFields()
* Field getDeclaredField(String name)
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
3. 获取成员方法们:
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
4. 获取类名
* String getName()
*/
//首先,获取Person的Class对象
//只能获取所有public修饰的成员变量
Class<Person> personClass = Person.class;
/**
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*/
//这些方法是使用public 修饰的
Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
System.out.println(constructor);
System.out.println("---------------------------");
Person person = constructor.newInstance("丫丫", 23);
System.out.println(person);
System.out.println("---------------------------");
Constructor<Person> constructor1 = personClass.getConstructor();
System.out.println(constructor1);
Person person1 = constructor1.newInstance();
System.out.println(person1);
//这些方法是使用private 修饰的
System.out.println("---------------------------");
Class<PersonPrivate> personPrivateClass = PersonPrivate.class;
Constructor<PersonPrivate> declaredConstructor = personPrivateClass.getDeclaredConstructor(String.class, int.class);
System.out.println(declaredConstructor);
declaredConstructor.setAccessible(true);//这行代码如果放到赋值下面 会报IllegalAccessException
PersonPrivate personPrivate = declaredConstructor.newInstance("丫丫1", 24);
System.out.println(personPrivate);
System.out.println("---------------------------");
//获取所有构造方法
Constructor<?>[] declaredConstructors = personPrivateClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor1 : declaredConstructors) {
System.out.println(declaredConstructor1);
}
}
}
运行结果:
public domain.Person(java.lang.String,int)
---------------------------
Person{name='丫丫', age=23, a='null', b='null', c='null', d='null'}
---------------------------
public domain.Person()
Person{name='null', age=0, a='null', b='null', c='null', d='null'}
---------------------------
private domain.PersonPrivate(java.lang.String,int)
Person{name='丫丫1', age=24, a='null', b='null', c='null', d='null'}
---------------------------
private domain.PersonPrivate(java.lang.String,int)
private domain.PersonPrivate()
package test;
import domain.Person;
import domain.PersonPrivate;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class ReflectDemo4 {
public static void main(String[] args) throws Exception {
/**
Class对象功能:
* 获取功能:
1. 获取成员变量们
* Field[] getFields()
* Field getField(String name)
* Field[] getDeclaredFields()
* Field getDeclaredField(String name)
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
3. 获取成员方法们:
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
4. 获取类名
* String getName()
*/
//首先,获取Person的Class对象
Class<Person> personClass = Person.class;
/**
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*/
//获取指定名称的方法
Method eatMethod = personClass.getMethod("eat");
Person person=new Person();
//执行方法 这是我们经常使用的
eatMethod.invoke(person);
System.out.println("---------------------------");
Method eatMethod2 = personClass.getMethod("eat",String.class);
eatMethod2.invoke(person,"西瓜");
System.out.println("---------------------------");
//获取所有public 修饰的方法
Method[] declaredMethods = personClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
//同时获取方法的名称
String name = declaredMethod.getName();
System.out.println(name);
// declaredMethod.setAccessible(true);
}
System.out.println("---------------------------");
Method drinkMethod = personClass.getDeclaredMethod("drink",String.class);
drinkMethod.setAccessible(true);
Person person1 = personClass.newInstance();
drinkMethod.invoke(person1,"饮料");
}
}
运行结果:
eat====
---------------------------
eat西瓜
---------------------------
public java.lang.String domain.Person.toString()
toString
public java.lang.String domain.Person.getName()
getName
public void domain.Person.setName(java.lang.String)
setName
private void domain.Person.drink()
drink
private void domain.Person.drink(java.lang.String)
drink
public void domain.Person.eat(java.lang.String)
eat
public void domain.Person.eat()
eat
public void domain.Person.setAge(int)
setAge
public int domain.Person.getAge()
getAge
---------------------------
drink饮料
package test;
import domain.Person;
import java.lang.reflect.Method;
/**
* Created by ZhaoYongqiang on 2020/9/8.
*/
public class ReflectDemo5 {
public static void main(String[] args) throws Exception {
/**
Class对象功能:
* 获取功能:
1. 获取成员变量们
* Field[] getFields()
* Field getField(String name)
* Field[] getDeclaredFields()
* Field getDeclaredField(String name)
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
3. 获取成员方法们:
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
4. 获取类名
* String getName()
*/
//首先,获取Person的Class对象
Class<Person> personClass = Person.class;
/**
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*/
//获取类名 在实际的项目开发中也是经常用到的
String name = personClass.getName();
System.out.println("============="+name);
}
}
运行结果:
=============domain.Person