Why does LongProperty implement Property but not Property?

前端 未结 1 697
梦如初夏
梦如初夏 2020-12-04 03:25

I have come across what seems like a peculiarity in the JavaFX API: LongProperty implements Property, but not Property

相关标签:
1条回答
  • 2020-12-04 03:48

    It can't implement both.

    To do that, it would need to implement two versions of each method in the interface that uses the generic. Let's take one as an example:

    bindBidirectional(Property<Long> other) { ... }
    

    Under the hood, erasure means this gets compiled down to:

    bindBidirectional(Property other) { ... }
    

    So then, what would something that implements Property<Number> and Property<Long> do? It would have two methods:

    bindBidirectional(Property<Long> other) { ... }
    bindBidirectional(Property<Number> other) { ... }
    

    ... that would compile down, after erasure, to two methods:

    bindBidirectional(Property other) { ... }
    bindBidirectional(Property other) { ... }
    

    These two methods conflict, and there'd be no way to resolve them at runtime.

    Even if you used some compiler trickery to get around this, what happens when someone uses LongProperty as a raw Property?

    Property rawLongProperty = new LongProperty();
    rawLongProperty.bindBidirectional(someOtherRawProperty);
    

    There's no way to know which of the two bindDirectional variants this is meant to resolve to.

    0 讨论(0)
提交回复
热议问题