Java 将内部具有FloatProperty的ObjectProperty绑定到其他属性

Java 将内部具有FloatProperty的ObjectProperty绑定到其他属性,java,javafx,javabeans,Java,Javafx,Javabeans,我有一篇文章类,里面有一些属性 public class Article { private IntegerProperty id; private StringProperty name; private StringProperty description; private FloatProperty price; public Article(int id, String name, String description, float price)

我有一篇
文章
类,里面有一些
属性

public class Article {
    private IntegerProperty id;
    private StringProperty name;
    private StringProperty description;
    private FloatProperty price;

    public Article(int id, String name, String description, float price) {
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty(name);
        this.description = new SimpleStringProperty(description);
        this.price = new SimpleFloatProperty(price);
    }

    ...
}
我有一个
Sale
类,它引用了一篇文章

public class Sale {
    private IntegerProperty id;
    private ObjectProperty<Article> article;
    private FloatProperty discount;
    private IntegerProperty quantity;
    private FloatProperty total;

    public Sale(int id, Article article, float discount, int quantity) {
        this.id = new SimpleIntegerProperty(id);
        this.article = new SimpleObjectProperty<>(article);
        this.discount = new SimpleFloatProperty(discount);
        this.quantity = new SimpleIntegerProperty(quantity);
        this.checked = new SimpleBooleanProperty(false);

        this.total = new SimpleFloatProperty();
        this.total.bind(Bindings.multiply(Bindings.subtract(this.article.get().priceProperty(), Bindings.multiply(this.article.get().priceProperty(), this.discount)), this.quantity));
    }

    public void setArticle(Article article) {
        this.article.set(article);
    }
    ...
}
我想我应该引用articleProperty而不是值,但我不知道如何引用。

您可以:

public Sale(int id, Article article, float discount, int quantity) {
    this.id = new SimpleIntegerProperty(id);
    this.article = new SimpleObjectProperty<>(article);
    this.discount = new SimpleFloatProperty(discount);
    this.quantity = new SimpleIntegerProperty(quantity);
    this.checked = new SimpleBooleanProperty(false);

    this.total = new SimpleFloatProperty();

    FloatProperty articlePriceProperty = new SimpleFloatProperty();
    if (this.article != null) {
        articlePriceProperty.bind(article.priceProperty());
    }
    this.article.addListener((obs, oldArticle, newArticle) -> {
        articlePriceProperty.unbind();
        if (newArticle == null) {
            articlePriceProperty.set(0);
        } else {
            articlePriceProperty.bind(newArticle.priceProperty());
        }
    });
    this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty, Bindings.multiply(articlePriceProperty(), this.discount)), this.quantity));
}
公开销售(整数id、物品、浮动折扣、整数数量){
this.id=新的SimpleIntegerProperty(id);
this.article=新的SimpleObject属性(article);
this.discount=新的SimpleFloatProperty(折扣);
this.quantity=新的SimpleIntegerProperty(数量);
this.checked=新的SimpleBoleAnProperty(false);
this.total=新的SimpleFloatProperty();
FloatProperty articlePriceProperty=新的SimpleFloatProperty();
if(this.article!=null){
articlePriceProperty.bind(article.priceProperty());
}
this.article.addListener((obs、oldArticle、newArticle)->{
articlePriceProperty.unbind();
if(newArticle==null){
articlePriceProperty.set(0);
}否则{
articlePriceProperty.bind(newArticle.priceProperty());
}
});
this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty,Bindings.multiply(articlePriceProperty(),this.discount)),this.quantity));
}
您可以使用:


在减法中1d的含义是什么?您的总公式:
(价格-(价格*折扣))*数量
挖掘一个等价公式,避免在两个位置使用价格(使用分配规则):
价格*数量*(1-折扣)
工作完美!谢谢:D(我只是讨厌
绑定。选择(…)
public Sale(int id, Article article, float discount, int quantity) {
    this.id = new SimpleIntegerProperty(id);
    this.article = new SimpleObjectProperty<>(article);
    this.discount = new SimpleFloatProperty(discount);
    this.quantity = new SimpleIntegerProperty(quantity);
    this.checked = new SimpleBooleanProperty(false);

    this.total = new SimpleFloatProperty();

    FloatProperty articlePriceProperty = new SimpleFloatProperty();
    if (this.article != null) {
        articlePriceProperty.bind(article.priceProperty());
    }
    this.article.addListener((obs, oldArticle, newArticle) -> {
        articlePriceProperty.unbind();
        if (newArticle == null) {
            articlePriceProperty.set(0);
        } else {
            articlePriceProperty.bind(newArticle.priceProperty());
        }
    });
    this.total.bind(Bindings.multiply(Bindings.subtract(articlePriceProperty, Bindings.multiply(articlePriceProperty(), this.discount)), this.quantity));
}
this.total.bind(Bindings.multiply(Bindings.selectFloat(this.article, "price")
                                          .multiply(this.quantity),
                                  Bindings.subtract(1d, this.discount)));