反射入门
实体类
package com.gzjc.lotteryol.util.reflex.dome;
/**
* @author jianglinchen
* @description 实体类
* @date 2020/1/7 / 16:27
*/
public class Person {
private String name;
private Integer age;
protected Integer test1;
private Integer test2;
public Integer test;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void cat() {
System.out.println("小猫咪");
}
public void cat(String food) {
System.out.println("小猫咪在吃: " + food);
}
}
ReflexTest类
package com.gzjc.lotteryol.util.reflex;
import com.gzjc.lotteryol.util.reflex.dome.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author jianglinchen
* @description 放射
* @date 2020/1/6 / 17:47
*/
public class ReflexTest {
public static void main(String[] args) {
try {
System.out.println("\n获取Class对象的方式 ================================================");
/*
获取Class对象的方式
1.Class.forName("全类名"): 将字节码文件加载到内存,返回class对象
2.类名.class: 通过类名的属性class获取
3.对象.getClass(): getClass()方法在Object类中定义
*/
/*
全类名:
多用于配置文件,将类名定义在配置文件中,读取文件,加载类
*/
Class cls1 = Class.forName("com.gzjc.lotteryol.util.reflex.dome.Person");
System.out.println(cls1);
/*
2.类名.class:
多用于参数的传递
*/
Class cls2 = Person.class;
System.out.println(cls2);
/*
3.对象.getClass():
多用于对象的获取字节码方式
*/
Person person = new Person();
Class cls3 = person.getClass();
System.out.println(cls3);
// 比较3个对象
System.out.println(cls1 == cls2);
System.out.println(cls1 == cls3);
System.out.println(cls2 == cls3);
/*
结论:
同一个字节码文件(*.class)在一次程序运行中,只会被加载一次,无论用哪个方法获取Class对象,都是同一个
*/
/*
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(Stirng name,类<?>... parameterTypes)
4.获取类名
String getName()
*/
System.out.println("\n获取成员变量们 ================================================");
// 1.获取Person的Class对象
Class cls = Person.class;
// 2.获取成员变量们
Field[] fields = cls.getFields();
// 快捷 iter
System.out.println("\ngetFields 获取public修饰的成员变量们");
for (Field field : fields) {
System.out.println(field);
}
System.out.println("\ngetField 获取指定名称的 public修饰的成员变量");
Field test = cls.getField("test");
System.out.println(test);
person.test = 10;
Object o1 = test.get(person);
System.out.println(o1);
Field[] declaredFields = cls.getDeclaredFields();
// 快捷 iter
System.out.println("\ngetDeclaredFields 获取成员变量们, 无视修饰符");
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
System.out.println("\ngetDeclaredField 获取指定成员变量, 无视修饰符");
Field test1 = cls.getDeclaredField("age");
System.out.println(test1);
// 忽略访问权限修饰符的安全检查
test1.setAccessible(true);//暴力方式
person.setAge(23);
Object o = test1.get(person);
System.out.println(o);
System.out.println("\n获取构造方法们 ================================================");
Class persionClss = Person.class;
System.out.println("\ngetConstructors 获取构造方法们");
Constructor[] constructors = persionClss.getConstructors();
System.out.println(constructors.length);
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
System.out.println("\ngetConstructor 获取指定的构造方法");
Constructor constructor = persionClss.getConstructor(String.class, Integer.class);
Object lc = constructor.newInstance("凌晨", 18);
System.out.println(lc);
Object o2 = persionClss.newInstance();
System.out.println(o2);
System.out.println("\n获取成员方法们 ================================================");
Class persionClss2 = Person.class;
System.out.println("\ngetMethods 获取public成员方法们");
Method[] methods = persionClss2.getMethods();
for (Method method : methods) {
System.out.println(method);
String name = method.getName();
System.out.println(name);
}
System.out.println("\ngetMethod 获取指定public成员方法");
Method cat = persionClss2.getMethod("cat");
System.out.println("invoke 执行方法");
cat.invoke(new Person());
Method cat1 = persionClss2.getMethod("cat", String.class);
System.out.println("invoke 执行方法");
cat1.invoke(new Person(), "饭");
} catch (Exception e) {
e.printStackTrace();
}
}
}