Java Casting ArrayIterator<;对象>;

Java Casting ArrayIterator<;对象>;,java,casting,Java,Casting,queryMe方法返回一个ArrayIterator。queryYou方法返回一个ArrayIterator 公共数组迭代器查询(字符串表、字符串字段、字符串条件) { ArrayIterator结果=空; if(表等于(“我的表”) { 结果=MyTable.queryMe(字段、标准); } else if(table.equals(“YourTable”) { 结果=YourTable.queryYou(字段、标准); } 返回结果; } 我得到一个错误,上面写着 ArrayIterat

queryMe方法返回一个ArrayIterator。queryYou方法返回一个ArrayIterator

公共数组迭代器查询(字符串表、字符串字段、字符串条件)
{
ArrayIterator结果=空;
if(表等于(“我的表”)
{
结果=MyTable.queryMe(字段、标准);
}
else if(table.equals(“YourTable”)
{
结果=YourTable.queryYou(字段、标准);
}
返回结果;
}
我得到一个错误,上面写着

ArrayIterator<Me> and ArrayIterator<Java.lang.object> are incompatible types.
ArrayIterator和ArrayIterator是不兼容的类型。

有什么建议吗?

您不能强制执行它,因为
ArrayIterator
不是
ArrayIterator
的子类型事实上,这两种类型并不相关


.

您不能将
ArrayIterator
转换为
ArrayIterator
,您应该更改
queryMe
函数的返回类型
但是如果你是迭代器,那么你最好在你的整个程序中使用
ArrayIterator
,这是你要解决的问题。先把它转换成一个原始的ArrayIterator,然后再转换成ArrayIterator

ArrayIterator meIter=(ArrayIterator)结果

更好的方法是更改方法以返回ArrayIterator,并将结果更改为相同

***刚刚看到您的更新。该方法似乎试图返回不同类型的ArrayInterators,因此返回类型为

// 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.
}
//如果这两种类型共享一个通用的超级类型就好了。
公共ArrayInterator查询(字符串表、字符串字段、字符串条件)
{
//警告,这是原始泛型类型,不提供类型安全性
ArrayIterator结果=空;
if(表等于(“我的表”)
{
结果=MyTable.queryMe(字段、标准);
}
else if(table.equals(“YourTable”)
{
结果=YourTable.queryYou(字段、标准);
}
返回(ArrayIterator)结果。
}

正如error所说,我认为您应该创建并返回类型为的ArrayIterator,而不是返回类型为object的ArrayIterator该方法要复杂得多,它还有许多其他if语句。为了简单起见,我只发布了一条。我现在就要编辑它。认为ArrayIteratoryIterator是ArrayIterator的一个子类,因为Me是Object的一个子类。但它不是这样工作的。任何关于泛型的教程都可以解释为什么。我要怎么做才能解决这个问题?我尝试过,让方法返回ArrayIterator,但那不起作用。这个类只有静态方法,Java不允许您引用t from是一个静态上下文。
// 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.
}