一、反射的定义
二、获取Class对象三种方式
全类名:包名+类名。
public class test {
public static void main(String [] args) throws ClassNotFoundException {
//第一种方式
Class class1=Class.forName("test02.Student");
//第二种方法
Class class2=Student.class;
//第三种方法
Student student=new Student();
Class class3=student.getClass();
System.out.println(class1==class2);
System.out.println(class2==class3);
System.out.println(class1);
}
}
三、利用反射获取构造方法
public class test {
public static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
//获取class字节码对象文件
Class class1=Class.forName("test02.Student");
//获取所有构造方法
Constructor[] css=class1.getDeclaredConstructors();
for(Constructor c:css) {
System.out.println(c);
}
System.out.println("-------------");
//获取单个构造方法
Constructor con1= class1.getDeclaredConstructor(String.class);
System.out.println(con1);
}
}
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name) {
this.setName(name);
}
protected Student(String name,int age) {
this.setName(name);
this.setAge(age);
}
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 "Student [name=" + name + ", age=" + age + "]";
}
}
四、利用反射获取成员变量
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name,int age) {
this.setName(name);
this.setAge(age);
}
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 "Student [name=" + name + ", age=" + age + "]";
}
}
public class test {
public static void main(String [] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
//获取class字节码对象文件
Class class1=Class.forName("test02.Student");
//获取所有成员变量
Field[] fi= class1.getDeclaredFields();
for(Field f:fi) {
System.out.println(f);
}
System.out.println("--------------");
//获取单个成员变量
Field fii= class1.getDeclaredField("name");
System.out.println(fii);
Student s=new Student("zhangsan",23);
fii.setAccessible(true);
Object r= fii.get(s);
System.out.println(r);
}
}
五、利用反射获取成员方法
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name,int age) {
this.setName(name);
this.setAge(age);
}
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 void eat(String something) {
System.out.println("在吃"+something);
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class test {
public static void main(String [] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取class字节码对象文件
Class class1=Class.forName("test02.Student");
//获取指定的成员方法
Method method= class1.getDeclaredMethod("eat", String.class);
System.out.println(method);
//创建对象
Student s=new Student();
method.invoke(s, "汉堡");
}
}
六、综合练习
练习1:信息保存
对于任意一个对象,都可以把对象所有的字段名和值,保存到文件中去。
分析:可以利用反射,将成员变量获取出来,再利用IO流将获取出来的变量,写入文件中。
Student类:
public class Student {
private String name;
private int age;
private String gender;
private double height;
private String hobby;
public Student() {
}
public Student(String name,int age,String gender,double height,String hobby) {
this.setName(name);
this.setAge(age);
this.setGender(gender);
this.setHeight(height);
this.setHobby(hobby);
}
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;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" + gender + ", height=" + height + ", hobby="
+ hobby + "]";
}
}
Teacher类:
public class Teacher {
private String name;
private int salary;
public Teacher() {
}
public Teacher(String name,int salary) {
this.setName(name);
this.setSalary(salary);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", salary=" + salary + "]";
}
}
测试类:
package test02;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
public class test {
public static void main(String [] args) throws IllegalArgumentException, IllegalAccessException, IOException{
Student s=new Student("李明",23,"女",92.5,"画画");
Teacher t=new Teacher("王老师",8500);
Get(s);
}
//创建一个方法获取成员变量
public static void Get(Object obj) throws IllegalArgumentException, IllegalAccessException, IOException {//Object是获取对象
//获取字节码文件
Class cla=obj.getClass();
//创建IO流
BufferedWriter bw=new BufferedWriter(new FileWriter("c.txt"));
//获取每个成员变量
Field[] fields=cla.getDeclaredFields();
//利用循环获取成员变量
for(Field field:fields) {
field.setAccessible(true);
String name=field.getName();
Object vaule= field.get(obj);
bw.write(name+"="+vaule);
bw.newLine();
}
bw.close();
}
}