/**
 * 在这里给出对类 Student 的描述。
 * 
 * @作者(你的名字)
 * @版本(一个版本号或者一个日期)
 */
public class Student
{
    // 实例变量 - 用你自己的变量替换下面的例子
    private int x;
    private int id;
    private String name;
    private boolean male;
    private double account;
    /**
     * 类 Student 的对象的构造函数
     */
    public Student()
    {
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name=name;
    }

    public int getId(){
        return this.id;
    }

    public void setId(int id){
        this.id=id;
    }

    public boolean getMale(){
        return this.male;
    }

    public void setId(boolean male){
        this.male=male;
    }

    public double getAccount(){
        return this.account;
    }

    public void setId(double account){
        this.account=account;
    }
}

  

/**
 * 利用发射机制,修改类的私有域
 * 
 * @作者(你的名字)
 * @版本(一个版本号或者一个日期)
 */
import java.util.*;
import java.lang.reflect.Field;
public class Test
{
    public static void main(String[]args){
        Student student=new Student();
        Class<?>clazz=student.getClass();
        System.out.println("class standard name:"+clazz.getCanonicalName());
        try{
            Field id=clazz.getDeclaredField("id");
            System.out.println("before id change:"+student.getId());
            id.setAccessible(true);
            //对于私有域,一定要使用setAccessible()方法将其设置为true才能设置新值
            id.setInt(student,10);
            System.out.println("after id change:"+student.getId());

            Field name=clazz.getDeclaredField("name");
            System.out.println("before name change:"+student.getName());
            name.setAccessible(true);
            name.set(student,"xiaoqiang");
            System.out.println("after name change:"+student.getName());

            Field male=clazz.getDeclaredField("male");
            System.out.println("before male change:"+student.getMale());
            male.setAccessible(true);
            male.setBoolean(student,true);
            System.out.println("after male change:"+student.getMale());

            Field account=clazz.getDeclaredField("account");
            System.out.println("before account change:"+student.getAccount());
            account.setAccessible(true);
            account.setDouble(student,12.34);
            System.out.println("after account change:"+student.getAccount());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}