反射的定义:在Java中类是对象的模板,而反射就是要通过对象来找出类的构造方法,属性,类名,参数等。
一,建立一个Student类 并生成他的构造方法 等一系列方法
public class Student { public String name; public int age; public String gender; public Student(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name) && Objects.equals(gender, student.gender); } @Override public int hashCode() { return Objects.hash(name, age, gender); } 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; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } private Student() { this.name = name; this.age = age; this.gender = gender; } }
二,通过对象找出类名,包名,共有三种方法
public class ReflectSarter { public static void main(String[] args) throws ClassNotFoundException { // Student student = new Student(); //第一种方式 // Class<? extends Student> stu1 = student.getClass(); //第二种方法 Class<Student> stu2 = Student.class; //第三种方法 Class<?> stu3 = Class.forName("com.mbiletrin.Student"); // System.out.println(stu1); System.out.println(stu2); System.out.println(stu3); } }
三,通过对象找出构造器
public class RefletSecond { public static void main(String[] args) throws NoSuchMethodException { Class<Student> stuclz = Student.class; // Constructor<Student> const1 = stuclz.getConstructor(); Constructor<Student> const2 = stuclz.getConstructor(String.class, int.class, String.class); Constructor<?>[] const3 = stuclz.getConstructors(); // System.out.println(const1); System.out.println(const2); System.out.println(Arrays.asList(const3)); Constructor<Student> dcont1 = stuclz.getDeclaredConstructor(); Constructor<Student> dcont2 = stuclz.getDeclaredConstructor(String.class, int.class, String.class); Constructor<?>[] dcont3 = stuclz.getDeclaredConstructors(); System.out.println(dcont1); System.out.println(dcont2); System.out.println(Arrays.asList(dcont3)); } }
四,通过对象去找属性
public class ReflFourth { public static void main(String[] args) throws NoSuchFieldException { Class<Student> stuclz = Student.class; Field name = stuclz.getField("name"); Field[] fields = stuclz.getFields(); System.out.println(name); System.out.println(Arrays.asList(fields)); Field gender = stuclz.getDeclaredField("gender"); Field[] declaredFields = stuclz.getDeclaredFields(); System.out.println(gender); System.out.println(Arrays.asList(declaredFields)); } }
五,通过对象去找方法
public class ReflecTird { public static void main(String[] args) throws NoSuchMethodException { Class<Student> stud = Student.class; Method getName = stud.getMethod("getName"); Method setName = stud.getMethod("setName", String.class); Method[] methods = stud.getMethods(); System.out.println(getName); System.out.println(setName); System.out.println(Arrays.asList(methods)); System.out.println("--------------------------------------------------"); Method method = stud.getDeclaredMethod("getAge"); Method getName1 = stud.getDeclaredMethod("getName"); Method[] m3 = stud.getDeclaredMethods(); System.out.println(method); System.out.println(getName1); System.out.println(Arrays.asList(m3)); } }