package com.zjw.反射;
import com.zjw.注解.MyAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class DoctorTest {
public static void main(String[] args) throws Exception {
/*
通过反射类拿到对应类的属性
getDeclaredFields(); 拿到本类所有属性
getFields(); 拿到本类与父类所有public属性
*/
//通过第二种方法拿到 Doctor的反射类对象 doctorClass
Class<Doctor> doctorClass = Doctor.class;
//通过反射类拿到本类中全部的属性成员对象
Field[] declaredFields = doctorClass.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println("Doctor DeclaredField-" + field);
}
/* Doctor DeclaredField-private java.lang.String com.zjw.反射.Doctor.name
Doctor DeclaredField-private java.lang.Integer com.zjw.反射.Doctor.age
Doctor DeclaredField-public java.lang.Integer com.zjw.反射.Doctor.IDCard
Doctor DeclaredField-java.lang.String com.zjw.反射.Doctor.sex */
//拿到本类中指定属性的属性成员对象
Field age = doctorClass.getDeclaredField("age");
System.out.println(age);
//private java.lang.Integer com.zjw.反射.Doctor.age
//通过反射类拿到本类以及父类中 public修饰的属性成员对象
Field[] fields = doctorClass.getFields();
for (Field field : fields) {
System.out.println("Doctor Field-" + field);
}
/* Doctor Field-public java.lang.Integer com.zjw.反射.Doctor.IDCard
Doctor Field-public java.lang.String com.zjw.反射.People.address */
//拿到本类中指定的属性成员对象
Field idCard = doctorClass.getField("IDCard");
System.out.println(idCard);
//public java.lang.Integer com.zjw.反射.Doctor.IDCard
//拿到父类中指定的属性成员对象
Field address = doctorClass.getField("address");
System.out.println(address);
//public java.lang.String com.zjw.反射.People.address
// 通过反射 newInstance 要赋值类对象
Doctor doctor = doctorClass.newInstance();
// 为 doctor 类 的 age 赋值 因为 age属性为私有属性所以需要设置允许访问
age.setAccessible(true); //设置允许访问私有属性
age.set(doctor,16); // 相当于 doctor.setAge(16);
System.out.println(doctor);
//Doctor(name=null, age=16, IDCard=null, sex=null)
//获取 age属性上的注解
MyAnnotation annotation = age.getAnnotation(MyAnnotation.class);
System.out.println(annotation.value());
/*
注意:自定义注解,设置@Retention(value = RetentionPolicy.RUNTIME)
要在运行时才能够显示注解内容,否则出现空指针异常
*/
System.out.println("----------------------------------");
/*
反射类获取方法对象
*/
//用第三种方式,通过对象获取反射类
Doctor newDoctor = new Doctor();
Class<? extends Doctor> aClass = newDoctor.getClass();
//通过反射创建对象
//Doctor insDoctor = aClass.newInstance();
//获取本反射类中 所有方法对象
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method);
}
/* 打印结果
public boolean com.zjw.反射.Doctor.equals(java.lang.Object)
public java.lang.String com.zjw.反射.Doctor.toString()
protected boolean com.zjw.反射.Doctor.canEqual(java.lang.Object)
public int com.zjw.反射.Doctor.hashCode()
public java.lang.String com.zjw.反射.Doctor.getName()
public void com.zjw.反射.Doctor.setName(java.lang.String)
public java.lang.Integer com.zjw.反射.Doctor.getAge()
public void com.zjw.反射.Doctor.setAge(java.lang.Integer)
public java.lang.Integer com.zjw.反射.Doctor.getIDCard()
public void com.zjw.反射.Doctor.setIDCard(java.lang.Integer)
public java.lang.String com.zjw.反射.Doctor.getSex()
public void com.zjw.反射.Doctor.setSex(java.lang.String)
public void com.zjw.反射.Doctor.fun01()
public void com.zjw.反射.Doctor.fun02()
private void com.zjw.反射.Doctor.fun03() */
//获取本类中指定的方法对象 无参
Method fun01 = aClass.getDeclaredMethod("fun01");
//执行方法
fun01.invoke(newDoctor);
//获取本类中指定的 私有方法对象 有参
Method fun03 = aClass.getDeclaredMethod("fun03", String.class,Integer.class);
//执行方法
fun03.setAccessible(true);
fun03.invoke(newDoctor,"周",18);
//获取本类及父类中 public修饰的全部方法对象
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
//结果不仅有本类及父类的 public修饰方法 ,还有 父类的父类 Object中的 public 方法
//获取本类中指定的 public修饰的方法
Method fun02 = aClass.getMethod("fun02", String.class);
//执行方法
fun02.invoke(newDoctor,"黄");
//获取父类中的 public修饰的方法
Method show = aClass.getMethod("show");
//执行方法
show.invoke(newDoctor);
System.out.println("------------------------------");
//通过第一种方法拿到 Doctor的反射类对象 doctorClass
Class<Doctor> forNameClass = (Class<Doctor>) Class.forName("com.zjw.反射.Doctor");
//创建对象时自动调用无参构造器 以及父类的无参构造器
Doctor newedInstance = forNameClass.newInstance();
//控制台:父类无参构造器
// 子类无参构造器
//获取本类所有构造器对象
Constructor<?>[] declaredConstructors = forNameClass.getDeclaredConstructors();
for (Constructor<?> constructor : declaredConstructors) {
System.out.println(constructor);
}
/* 打印结果:
public com.zjw.反射.Doctor(java.lang.String,java.lang.Integer,java.lang.Integer,java.lang.String)
private com.zjw.反射.Doctor(java.lang.Integer)
public com.zjw.反射.Doctor(java.lang.String)
public com.zjw.反射.Doctor() */
//得到无参构造器
Constructor<Doctor> declaredConstructor = forNameClass.getDeclaredConstructor();
//调用
Doctor doctor1 = declaredConstructor.newInstance();
/* 父类无参构造器
子类无参构造器 */
//得到指定有参构造函数
Constructor<Doctor> constructor1 = forNameClass.getDeclaredConstructor(String.class);
//调用
Doctor doctor2 = constructor1.newInstance("小小");
/* 父类无参构造器
有参构造器小小 */
//得到私有构造器
Constructor<Doctor> constructor2 = forNameClass.getDeclaredConstructor(Integer.class);
//调用
constructor2.setAccessible(true);
Doctor doctor3 = constructor2.newInstance(18);
/* 父类无参构造器
有参构造器--18 */
//获取本类所有 public构造器对象
Co