本帖最后由 何羡玉 于 2013-5-6 09:53 编辑
我想用反射的方法设置 b ,i,s,d 的默认值,可是按照我的代码打印出来的结果是0.0........0....null...false
我写的代码如下 哪里错了
package enhanceTest;
import java.lang.reflect.Field;
public class reflectPoint {
private boolean b;
private int i;
private String s;
private double d;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
/**
* @param args
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
// TODO Auto-generated method stub
reflectPoint re=new reflectPoint();
Field[] fields=reflectPoint.class.getFields();
for(Field field :fields)
{
if(field.getType()==boolean.class)
field.setBoolean(re, true);
else if(field.getType()==int.class)
field.setInt(re, 100);
else if(field.getType()==String.class)
field.set(re, " www.itheima.com");
else if (field.getType()==double.class)
field.setDouble(re, 0.01D);
}
System.out.println(re.getD()+"........"+re.getI()+"...."+re.getS()+"..."+re.isB());
}
} |