不知道你理解没有,你的程序我做了各种测试,如我上面那段话。
Stutend[code]package com.itheima.test;
public class Student {
public String name;
public int age;
public float score;
public Student(){
name = "默认人物";
age = 20;
score = 520;
}
public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
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 float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String toString() {
return name + "\t" + age + "\t" + score;
}
}[/code][code]import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class FieldDemo02 {
public static void main(String[] args) throws Exception {
Object per = null;
Class<?> c = Class.forName("com.itheima.test.Student");
per = c.newInstance();
Field[] field = per.getClass().getFields();
System.out.println(field.length);
for (Field fi : field) {
Object oldValue = fi.get(per);
System.out.println(oldValue);
}
Method met = c.getMethod("toString");
System.out.println(met.invoke(per));
}
private static <T> void fillmethod(T[] a,T obj){
for(int i=0;i<a.length;i++){
a[i]=obj;
}
}
}[/code] |