2012-12-13 100 views
0

未定義類參數我有這個功能在我的GWT項目:GWT:包括功能

private InputElement getInputElement(int rowIndex, int columnIndex, 
CellTable<MyClassA> cellTable) { 
     InputElement input = null; 
     if (isColumnEditable(columnIndex)) { 
      input = (InputElement) cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild(); 
     } 
     return input; 
    } 

如果想在最後一個參數是CellTable<MyClassB>再使用此功能,因爲代碼的其餘部分是一模一樣。我怎樣才能做到這一點?

+0

是MyClassA和MyClassB有關嗎? – PermGenError

+0

不是。但是對於這個功能應該是一樣的。正如你所看到的,我們不使用MyClass,僅僅使用cellTable函數。 – Arturo

回答

2

你可以寫下面的代碼 -

public interface MyClassInterface { ... }

public class MyClassA implements MyClassInterface { ... }

public class MyClassB implements MyClassInterface { ... }

private <T extends MyClassInterface> InputElement getInputElement(int rowIndex, int columnIndex, CellTable<T> cellTable)      
{    
    InputElement input = null;    
    if (isColumnEditable(columnIndex)) 
    { 
     input = (InputElement) cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild(); 
    } 
    return input; 

}

+0

它的工作原理。謝謝! – Arturo

2

您需要創建一個接口MyClassInterface並在兩個類中實現它。

public interface MyClassInterface { 

} 

public class MyClassA implements MyClassInterface { 
    ... 
} 
public class MyClassB implements MyClassInterface { 
    ... 
} 
private InputElement getInputElement(int rowIndex, int columnIndex, 
CellTable<? extends MyClassInterface> cellTable) { 
     InputElement input = null; 
     if (isColumnEditable(columnIndex)) { 
      input = (InputElement) cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild(); 
     } 
     return input; 
    } 
+0

CellTablePresenter類型中的方法getInputElement(int,int,CellTable )不適用於參數(int,int,CellTable ) – Arturo

+0

謝謝。只是做了你所說的。清空接口並在MyClassA和MyClassB上添加「implements MyClassInterface」。但是我得到上面的錯誤。和MyClassB類似。 – Arturo

+1

你必須在不同的文件中創建接口和類。 – Sam