queryMe方法返回一个ArrayIterator。queryYou方法返回一个ArrayIterator。
public ArrayIterator<Object> query(String table, String field, String criterion)
{
ArrayIterator<Object> result = null;
if (table.equals("MyTable")
{
result = MyTable.queryMe(field, criterion);
}
else if (table.equals("YourTable")
{
result = YourTable.queryYou(field, criterion);
}
return result;
}我收到一个错误,上面说
ArrayIterator<Me> and ArrayIterator<Java.lang.object> are incompatible types.有什么建议吗?
发布于 2013-04-15 01:41:32
这就是你要找的黑客。首先将其转换为原始ArrayIterator,然后转换为ArrayIterator。
ArrayIterator meIter = (ArrayIterator)结果。
更好的方法是更改您的方法以返回ArrayIterator,并将结果更改为相同的值。
*刚看到您的更新。该方法似乎试图返回不同类型的ArrayIterators,因此返回类型。
// Would be nice if the 2 types shared a common super type.
public ArrayIterator<Object> query(String table, String field, String criterion)
{
// WARNING, this is a raw generic type, and provides no type safety
ArrayIterator result = null;
if (table.equals("MyTable")
{
result = MyTable.queryMe(field, criterion);
}
else if (table.equals("YourTable")
{
result = YourTable.queryYou(field, criterion);
}
return (ArrayIterator<Object>) result.
}发布于 2013-04-15 01:33:01
您不能强制转换它,因为ArrayIterator<Me>实际上不是ArrayIterator<Java.lang.object>的子类型,这两个类型没有关系。
Find more explanation。
发布于 2013-04-15 01:35:06
你不能将ArrayIterator<Me>转换成ArrayIterator<Object>,你应该改变queryMe函数的返回类型,但是如果你的迭代器总是有Me类型,最好在整个程序中使用ArrayIterator<Me>
https://stackoverflow.com/questions/16002037
复制相似问题