五个 double 属性,相互有一定比例关系,随机输入其中一个属性,返回所有属性值。
还有更简洁方法吗。。。
public class Test {
private Double a;
private Double b;
private Double c;
private Double d;
private Double e;
private final Double a_b = 0.1;
private final Double b_c = 0.2;
private final Double c_d = 0.3;
private final Double d_e = 0.4;
private final Double e_a = 5.0;
public Test(String fieldName, Double x_y) {
Field[] fields = EnergyTransformation.class.getDeclaredFields();//获得所有属性名
try {
Method field = this.getClass().getMethod(fieldName, Double.class);
field.invoke(this, x_y);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
for (int i = 0; i < 2; i++) {
this.a = this.a == null ? this.e == null ? null : this.e * this.e_a : this.a;
this.b = this.b == null ? this.a == null ? null : this.a * this.a_b : this.b;
this.c = this.c == null ? this.b == null ? null : this.b * this.b_c : this.c;
this.d = this.d == null ? this.c == null ? null : this.c * this.c_d : this.d;
this.e = this.e == null ? this.d == null ? null : this.d * this.d_e : this.e;
}
}
//省略getter setter...
}