反射によるオブジェクトのプロパティの取得とプロパティ値の変更
1、エンティティークラス
2、属性値の取得
運転結果:氏名:張三、年齢19
3、属性値の変更
運行結果:氏名:李四、年齢28
package com.ljb.app.model;
import java.io.Serializable;
/**
*
* @author LJB
* @version 2015 2 10
*/
public class Student implements Serializable{
private String name;
private int age;
private String gender;
private transient String password;
public Student () {};
public Student(String name, int age, String gender , String password) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.password = password;
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" + gender
+ "]";
}
}
2、属性値の取得
package com.ljb.app.my_first_maven;
import java.lang.reflect.Field;
import com.ljb.app.model.Student;
/**
*
* @author LJB
* @version 2015 3 2
*/
public class FieldCallTest {
/**
* @param args
*/
public static void main(String[] args) {
//
Student stu = new Student();
stu.setName(" ");
stu.setAge(19);
//
Class cla = Student.class;
//
try {
Field nameField = cla.getDeclaredField("name");
Field ageField = cla.getDeclaredField("age");
//
nameField.setAccessible(true);
ageField.setAccessible(true);
//
String name = (String) nameField.get(stu);
int age = ageField.getInt(stu);
System.out.println(" :" + name + ", " + age);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
運転結果:氏名:張三、年齢19
3、属性値の変更
package com.ljb.app.my_first_maven;
import java.lang.reflect.Field;
import com.ljb.app.model.Student;
/**
*
* @author LJB
* @version 2015 3 2
*/
public class FieldCallTest {
/**
* @param args
*/
public static void main(String[] args) {
//
Student stu = new Student();
stu.setName(" ");
stu.setAge(19);
//
Class cla = Student.class;
//
try {
Field nameField = cla.getDeclaredField("name");
Field ageField = cla.getDeclaredField("age");
//
nameField.setAccessible(true);
ageField.setAccessible(true);
//
nameField.set(stu, " ");
String modName = (String) nameField.get(stu);
ageField.set(stu, 28);
int modAge = ageField.getInt(stu);
System.out.println(" :" + modName + ", " + modAge);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
運行結果:氏名:李四、年齢28