构造函数,三目运算

五个 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...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值