Student student=new Student();
student.setId("12345");
student.setStuName("123");
student.setStuNum("bbb");
student.setStuAge(22);
student.setStuSex("F");
student.setEmail("aaaaaaaaaa");
Field[] fields=student.getClass().getDeclaredFields();
int temp=fields.length;
System.out.println(temp);
for(Field field :fields){

String name=field.getName();
System.out.println(name);
String str;
try {
//str = field.get(name).toString(); //我想根据属性名称获得值应该怎么写,例如知道有id这个属性,就能查到12345,但不是用student.getId() System.out.println(str);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

解决方案 »

  1.   

    如果属性是public就可以这样使用Field f = student.getClass().getField("id");
    Object strId= f.get(student);
    System.out.println("id的值:"+strId);如果属性是private的记得
    根据反射得到id属性对应的getId()的Method对象
    调用此对象的invoke()方法
    具体lz可以查看API操作
      

  2.   


    我的属性是私有的,看来了API还是有些糊涂. Method[] methods=student.getClass().getDeclaredMethods();
     for(Method m : methods)
     {
       System.out.println(m.getName());
       m.invoke(student, arg);//第一个参数:API写调用基础方法的对象,写student实例报wrong number of arguments
    写Student.class 报object is not an instance of declaring class,应该写什么,第二个参数:用于方法调用的参数
    我在这里是字段调用吗?这个参数尤其不懂
     }
     
    有知道的请指点下,谢谢啦
      

  3.   

    我以前写的,有价值,lz可以参考public class Student {
    private int studid; private String major; private double age; public Student() {
    super();
    } public Student(int studid, String major, double age) {
    super();
    this.studid = studid;
    this.major = major;
    this.age = age;
    } @Override
    public String toString() {
    return "";
    } public int getStudid() {
    return studid;
    } public void setStudid(int studid) {
    this.studid = studid;
    } public String getMajor() {
    return major;
    } public void setMajor(String major) {
    this.major = major;
    } public double getAge() {
    return age;
    } public void setAge(double age) {
    this.age = age;
    }
    }import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class ProduceSQL { /*
     * 通过JAVA的反射机制,给定一个Student的对象例如:
     * 
     * Student stud = new Student(1058, "计算机科学与技术", 22);
     * 
     * 需要根据给定的对象,构造出一条SQL语句:
     * 
     * insert into Student(studid,studname,age) values(1058,'计算机科学与技术',22);
     * 
     */
    public static void main(String[] args) {
    Student stud=new Student(1058,"计算机科学与技术", 22);
    productInsertSQL(stud);
    } public static String productInsertSQL(Student stud) {
    String result = "";
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("insert into "); System.out.println("Dubug: ---->>> " + stud.getClass());
    int position = stud.getClass().toString().indexOf("com");
    String fullpackagename = "";
    if (position != -1) {
    fullpackagename = stud.getClass().toString().substring(position);
    System.out.println("Dubug: ---->>> " + fullpackagename);
    } String tablename = fullpackagename.substring(fullpackagename
    .lastIndexOf(".") + 1); sbuf.append(tablename + "(");
    Class cs = null;
    try {
    cs = Class.forName(fullpackagename);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } Field field[] = cs.getDeclaredFields();
    for (int i = 0; i < field.length; i++) {
    String fieldname = field[i].getName();
    sbuf.append(fieldname + ",");
    } String mid = sbuf.toString();
    int lastpos = mid.lastIndexOf(",");
    if (lastpos != -1) {
    result = mid.substring(0, lastpos) + ")";
    } result += " values("; // 取值
    Method[] method = cs.getDeclaredMethods();
    StringBuffer valuesbuf = new StringBuffer();
    for (int i = 0; i < method.length; i++) { String methodname = method[i].getName();
    try {
    if (methodname.indexOf("get") != -1) {
    String tmpmethodname = method[i].getName(); Method methodd = cs.getMethod(tmpmethodname, null); // 对反射对象底层的方法进行调用,获得调用该方法所得到的值
    Object returnobject = methodd.invoke(stud, null);
    String tmpvalue = "";
    if (returnobject.getClass().getSimpleName()
    .equals("String")) {
    tmpvalue = "'" + returnobject.toString() + "'";
    } else {
    tmpvalue = returnobject.toString();
    } valuesbuf.append(tmpvalue + ","); }
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    }
    result += valuesbuf.toString(); int lastpos2 = result.lastIndexOf(",");
    if (lastpos2 != -1) {
    result = result.substring(0, lastpos2) + ")";
    }
    System.out.println(result);
    return result;
    }}