//Student.java public class Student { private String name; private int age;
public Student() { }
public Student(String name, int age) { this.name = name; this.age = age; }
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }
/** * 获取 * @return age */ public int getAge() { return age; }
/** * 设置 * @param age */ public void setAge(int age) { this.age = age; }
public String toString() { return "Student{name = " + name + ", age = " + age + "}"; } }
//Test.java public class Test { public static void main(String[] args) throws ClassNotFoundException { Class class1 = Class.forName("reflect.Student"); System.out.println(class1); Class class2 = Student.class; System.out.println(class2); Student student=new Student(); Class class3=student.getClass(); System.out.println(class3); System.out.println(class1==class2); System.out.println(class2==class3); } } /* class reflect.Student class reflect.Student class reflect.Student true true */
public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { Class clazz=Class.forName("reflect.Student"); //getFields Field[] fields1 = clazz.getFields(); for (Field field:fields1){ System.out.println(field); } System.out.println();
public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class clazz=Class.forName("reflect.Student"); //getMethods Method[] methods1 = clazz.getMethods(); for (Method method:methods1){ System.out.println(method); } System.out.println();
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }
/** * 获取 * @return age */ public int getAge() { return age; }
/** * 设置 * @param age */ public void setAge(int age) { this.age = age; }
public String toString() { return "Student{name = " + name + ", age = " + age + "}"; } }
/* public java.lang.String reflect.Student.toString() public java.lang.String reflect.Student.getName() public void reflect.Student.setName(java.lang.String) public int reflect.Student.getAge() public void reflect.Student.setAge(int) public void reflect.Student.study(java.lang.String) throws java.io.IOException,java.lang.ArrayIndexOutOfBoundsException public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll()
public java.lang.String reflect.Student.toString() public java.lang.String reflect.Student.getName() public void reflect.Student.setName(java.lang.String) public int reflect.Student.getAge() public void reflect.Student.setAge(int) private java.lang.String reflect.Student.eat(java.lang.String) private java.lang.String reflect.Student.eat(java.lang.String,java.lang.String) public void reflect.Student.study(java.lang.String) throws java.io.IOException,java.lang.ArrayIndexOutOfBoundsException
public void reflect.Student.study(java.lang.String) throws java.io.IOException,java.lang.ArrayIndexOutOfBoundsException
public class Test { public static void main(String[] args) throws IOException, IllegalAccessException { Student s=new Student("张三",18); Teacher t=new Teacher("李四",19999.0,3,11); //saveFile(t); //saveFIle(s)
} public static void saveFile(Object obj) throws IOException, IllegalAccessException { BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt")); Class clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field:fields){ field.setAccessible(true); String name = field.getName(); Object value = field.get(obj); bw.write(name+"="+value); bw.newLine(); } bw.close(); } }
//Student.java public class Student { private String name; private int age;
public Student() { }
public Student(String name, int age) { this.name = name; this.age = age; }
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }
/** * 获取 * @return age */ public int getAge() { return age; }
/** * 设置 * @param age */ public void setAge(int age) { this.age = age; }
public String toString() { return "Student{name = " + name + ", age = " + age + "}"; } } //Teacher.java public class Teacher { private String name; private double salary; private int id; private int age;
public Teacher() { }
public Teacher(String name, double salary, int id, int age) { this.name = name; this.salary = salary; this.id = id; this.age = age; }
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }
//反射可以跟配置文件结合的方式,动态的创建对象,并调用方法 /*properties.properties classname=reflect.Teacher method=teach */ //Student.java public class Student { private String name; private int age;
public Student() { }
public Student(String name, int age) { this.name = name; this.age = age; }
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }
/** * 获取 * @return age */ public int getAge() { return age; }
/** * 设置 * @param age */ public void setAge(int age) { this.age = age; }
public String toString() { return "Student{name = " + name + ", age = " + age + "}"; } public void study(){ System.out.println("学习"); } } //Teacher.java public class Teacher { private String name; private double salary; private int id; private int age;
public Teacher() { }
public Teacher(String name, double salary, int id, int age) { this.name = name; this.salary = salary; this.id = id; this.age = age; }
/** * 获取 * @return name */ public String getName() { return name; }
/** * 设置 * @param name */ public void setName(String name) { this.name = name; }