一、實體類java
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 + "]"; } }
二、獲取屬性值app
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(); } } }
運行結果:姓名:張三,年齡19maven
三、修改屬性值ide
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(); } } }
運行結果:姓名:李四,年齡28this