考慮以下層次:爲什麼需要多種類型鑄造?
TopClass
|__ MiddleClass
|__ BottomClass
然後將下面的代碼是 - 當然不是必需的:
public BottomClass getBottom() {
return (BottomClass) (MiddleClass) getObject();
}
凡getObject()
返回BottomClass
類型的實例,但有TopClass
返回類型。
你可以有效地短路它,並直接投射到BottomClass
。
但這個代碼提出了我的眉頭:
在JavaFX的源碼包, 類:com.sun.javafx.scene.control.skin.ProgressIndicatorSkin
@Override
public StyleableProperty<Paint> getStyleableProperty(ProgressIndicator n) {
final ProgressIndicatorSkin skin = (ProgressIndicatorSkin) n.getSkin();
return (StyleableProperty<Paint>)(WritableValue<Paint>)skin.progressColor;
}
凡界面層次是:
WritableValue<T>
|__ StyleableProperty<T>
而且progressColor
是的類型210,實現StyleableProperty<Paint>
,而是存儲在ObjectProperty<Paint>
變量,像這樣:
private ObjectProperty<Paint> progressColor = new StyleableObjectProperty<Paint>(null)
任何線索這是怎麼回事呢?
您是否嘗試刪除中間演員?它有錯誤嗎?錯誤說了什麼? – ReyCharles
它似乎只是一些不必要的代碼,但我對JavaFX不太瞭解,不知道是否有某種看不見的原因。 –
這不是我的代碼,它是JDK的一部分。而且可能不需要JavaFX的知識。 – Mordechai