package cn.foxsand.day02;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 应用反射的API,获取类的信息(名称、属性、方法、构造器)
*/
public class Demo02 {
public static void main(String[] args) {
String path = "cn.foxsand.day02.UserBean";
try {
Class<?> clazz1 = Class.forName(path);
// 获取类的名称
System.out.println(clazz1.getName()); //获取全路径
System.out.println(clazz1.getSimpleName()); //仅获取类名
// 获取属性
/**
* Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
* this {@code Class} object.
* 仅返回public属性
*/
Field[] fields = clazz1.getFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i].getName());
}
/**
* Returns an array of {@code Field} objects reflecting all the fields
* declared by the class or interface represented by this
* {@code Class} object. This includes public, protected, default
* (package) access, and private fields, but excludes inherited fields.
* 返回所有属性(继承的属性例外)
*/
fields = clazz1.getDeclaredFields();
for (Field f:fields
) {
System.out.println("属性:" + f.getName());
}
Field f1 = clazz1.getDeclaredField("age");
System.out.println(f1.getName());
// 获取方法
Method[] methods = clazz1.getDeclaredMethods();
for (Method m:methods
) {
System.out.println(m.getName());
}
methods = clazz1.getMethods();
for (Method m:methods
) {
System.out.println("方法:" + m.getName());
}
Method m1 = clazz1.getDeclaredMethod("getAge");
System.out.println(m1.getName());
m1 = clazz1.getDeclaredMethod("setAge", int.class);
System.out.println(m1.getName());
// 获取构造器
Constructor[] constructors = clazz1.getDeclaredConstructors();
for (Constructor c:constructors
) {
System.out.println("构造器:" + c);
}
Constructor c = clazz1.getDeclaredConstructor(int.class , String.class , int.class);
System.out.println(c);
}catch (Exception e1){
e1.printStackTrace();
}
}
}
package cn.foxsand.day02;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 通过反射API,动态的操作:构造器、方法、属性
*/
public class Demo03 {
public static void main(String[] args) {
String path = "cn.foxsand.day02.UserBean";
try {
Class<UserBean> clazz1 = (Class<UserBean>)Class.forName(path);
// 通过动态调用构造方法,构造对象
// 1.调用Bean的无参构造器创建对象
UserBean userBean = clazz1.newInstance();
System.out.println(userBean);
// 2.调用指定的有参构造器创建对象
Constructor<UserBean> c1 = clazz1.getDeclaredConstructor(int.class , String.class , int.class);
userBean = c1.newInstance(100 , "你好" , 10);
System.out.println(userBean);
// 通过反射API调用方法
UserBean u2 = clazz1.newInstance();
Method method = clazz1.getDeclaredMethod("setName", String.class);
method.invoke(u2 , "张三");
System.out.println(u2);
// 通过反射API操作属性
Field f = clazz1.getDeclaredField("name");
/**
* * Set the {@code accessible} flag for this object to
* the indicated boolean value. A value of {@code true} indicates that
* the reflected object should suppress Java language access
* checking when it is used. A value of {@code false} indicates
* that the reflected object should enforce Java language access checks.
* 为ture时,私有属性也可以set。
* false时,不能操作私有属性
*/
f.setAccessible(true);
f.set(u2 , "李四");
System.out.println(u2);
}catch (Exception e1){
e1.printStackTrace();
}
}
}