【问题标题】:Overloading JavaFX Bindings重载 JavaFX 绑定
【发布时间】:2018-08-10 00:20:51
【问题描述】:

我正在绑定两个标签,通过对 Bindings.when() 的一系列嵌套调用来计算第二个标签的值。由于我连续 3 次对 3 个相似的标签执行完全相同的操作,有没有办法通过重载 Bindings 方法或 IntegerBinding 方法之一来简化此代码?

private IntegerProperty ipStrength, ipAgility, ipIntelligence;
private IntegerProperty ipStBonus, ipAgBonus, ipInBonus;


public RolePlayingCharacter() {

    ...

    ipStBonus.bind(Bindings.when(ipStrength.lessThan(5))
            .then(-1)
            .otherwise(Bindings.when(ipStrength.lessThan(9))
                    .then(0)
                    .otherwise(Bindings.when(ipStrength.lessThan(11))
                            .then(1)
                            .otherwise(2))));

    ipAgBonus.bind(Bindings.when(ipAgility.lessThan(5))
            .then(-1)
            .otherwise(Bindings.when(ipAgility.lessThan(9))
                    .then(0)
                    .otherwise(Bindings.when(ipAgility.lessThan(11))
                            .then(1)
                            .otherwise(2))));

    ipInBonus.bind(Bindings.when(ipIntelligence.lessThan(5))
            .then(-1)
            .otherwise(Bindings.when(ipIntelligence.lessThan(9))
                    .then(0)
                    .otherwise(Bindings.when(ipIntelligence.lessThan(11))
                            .then(1)
                            .otherwise(2))));

    ...

我发现我可以覆盖 IntegerBinding 类的 computeValue 方法,但这似乎没有任何帮助,因为无论如何我都必须做 3 次某些事情,每个标签一次:

    IntegerBinding ibStatBonus = new IntegerBinding() {
        {
            super.bind(ipStrength);
        }

        @Override
        protected int computeValue() {
            int iStatValue = ipStrength.get();
            if (iStatValue < 5) {
                return -1;
            } else if (iStatValue < 9) {
                return 0;
            } else if (iStatValue < 11) {
                return 1;
            } else {
                return 2;
            }
        }
    };

我希望能够做一些简单的事情,例如:

    ipStBonus.bind(ipStrength.calculateStatBonus());
    ipAgBonus.bind(ipAgility.calculateStatBonus());
    ipInBonus.bind(ipIntelligence.calculateStatBonus());

我该如何实现这样的事情?如何使 caclulateStatBonus 方法成为 IntegerProperty 可用的方法列表的一部分?

【问题讨论】:

    标签: javafx binding overriding overloading subclassing


    【解决方案1】:

    总是可以为此创建一个辅助方法:

    public static IntegerBinding createStatBonusBinding(final IntegerProperty source) {
        return Bindings.createIntegerBinding(() -> {
            int value = source.get();
            if (value < 5) {
                return -1;
            }
            if (value < 9) {
                return 0;
            }
            if (value < 11) {
                return 1;
            }
            return 2;
        }, source);
    }
    
    ipStBonus.bind(createStatBonusBinding(ipStrength));
    ipAgBonus.bind(createStatBonusBinding(ipAgility));
    ipInBonus.bind(createStatBonusBinding(ipIntelligence));
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多