2
我對Scala非常陌生,我試圖將一些JSF控制器轉換爲Scala來做一些測試。 代碼如下:在編譯時Scala類型與泛型不匹配
abstract class BaseListBean[TENTITY <: AnyRef, TID <: AnyRef] extends Serializable {
@BeanProperty var current:TENTITY = _
@BeanProperty var lazyDataModel: LazyDataModel[TENTITY] = _
def initLazyDataModel(): LazyDataModel[TENTITY] = {
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
override def load(first: Int, pageSize: Int, sortField: String, sortOrder: SortOrder, filters: util.Map[String, AnyRef]): util.List[TENTITY] = {
val paginationInfo:PaginationInfo = new PaginationInfo()
paginationInfo.setFromRecord(first)
paginationInfo.setPageSize(pageSize)
paginationInfo.setSortBy(Array(sortField))
paginationInfo.setDirections(if (SortOrder.ASCENDING == sortOrder) Array(Direction.Asc) else Array(Direction.Desc))
val advisoryPaginatedResult:PaginatedResult[TENTITY] = getBaseCRUD.read(paginationInfo)
setRowCount(advisoryPaginatedResult.getTotalItems.toInt)
advisoryPaginatedResult getItems
}
override def getRowKey(obj: TENTITY): TID = getEntityKey(obj)
}
return model;
}
def init() = {
setLazyDataModel(initLazyDataModel())
}
def getEntityKey(obj: TENTITY) : TID
def getBaseCRUD() : BaseCRUD[TENTITY, TID]
def customInit() = {
}
}
的錯誤是:
Error:(24, 34) type mismatch;
found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
required: org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
我尋找一個解決方案,但問題仍然remaign。我嘗試用更簡單的代碼重現像這樣:
abstract class ExampleGenerics[T <: AnyRef, V <: AnyRef] {
@BeanProperty var list:util.AbstractMap[T, V] = _
def initList() : util.AbstractMap[T, V] = {
val model:util.AbstractMap[T, V] = new util.AbstractMap[T, V]() {
override def entrySet(): util.Set[Entry[T, V]] = {
return null;
}
}
return model
}
def init() = {
setList(initList());
}
}
但是,這將工作。有人可以幫忙嗎?由於