我有一個泛型類:方法重載不能像使用Java泛型預計
public class Facet<C extends Component>
{
private final C child;
public Facet(C child) { this.child = child; }
public C getChild() { return this.child; }
public UIComponent getViewComponent() {
return PrimeFacesComponentFactory.create(facesContext, this.getChild());
}
}
我有一個組件工廠,也有一堆的方法,其中有幾個是這樣的:
public static UIComponent create(FacesContext fc, Component component) {
throw new UnsupportedOperationException("Method not yet implemented for class: " + component.getClass().getName());
}
public static UIRow create(FacesContext fc, Row modelRow) { ... }
public static UIColumn create(FacesContext fc, Column modelColumn) { ... }
NOTE:在每個工廠方法中,第二個參數是一個擴展Component的對象,返回的對象始終是UIComponent的一個子類。第一個工廠方法(引發異常的方法)是「catch all」,如果沒有爲特定組件編寫特定的工廠方法,則會調用該方法。
我在做什麼:正確的工廠方法應根據組件facet的子類型是什麼類型來調用。發生什麼事是第一種工廠方法是總是調用(例如引發異常的方法)。這對我來說是違反直覺的,因爲C的類型在運行時是已知的,C是「擴展Component的某種特定類型」。
調試,我在Facet#getViewComponent
設置斷點,調用它,當我知道facet.getChild()
將返回類型行的對象。所以在這個例子中C是Row。令我驚訝的是,當工廠方法被調用時,例如
PrimeFacesComponentFactory.create(facesContext, facet.getChild());
程序執行流向「catch all」工廠方法!但是,如果我明確投facet.getChild()如下:
PrimeFacesComponentFactory.create(facesContext, (Row)facet.getChild());
然後調用特定的方法。奇怪,因爲我簡單地/冗餘地將一行放到一行。
我讀過some about why this is happening,但它仍然朦朧和違反我的想法。無論如何,我該如何解決這個問題?
你可以在調用'create(..)'之前顯示'facet'聲明嗎?或者你的意思是'this.getChild()'? –
哦,對!我做了很多代碼簡化操作,讓示例更直接,而且,不是'facet.getChild()',而應該簡單地按照您的建議使用'this.getChild()'。我會更新這個問題。 –