为什么我的Java通用数组类型传输方法调用编译?

时间:2015-03-02 17:56:22

标签: java generics

我正在尝试创建一个允许我将所有项目放在一个数组中的系统,并使用工厂对象将它们转换为另一种类型的项目,并使用这些新对象填充第二个数组,然后返回那。我想用泛型做这一切,所以这可以在单行静态方法调用中完成。但是,我收到一个我无法解析的错误。这是(我希望)所有相关代码:

问题类有:

// ...

// "ArrayPP" is my take on ArrayList that has a lot of convenience methods.
// The constructor here uses varargs to initialize the array:
ArrayPP<String> myStrings = new ArrayPP<>("foo", "bar", "baz");
// MyCharSequenceImpl is just a class that implements CharSequence.
ArrayPP<MyCharSequenceImpl> myCSI = new ArrayPP<>();
// The error occurs on the following line:
myCSI = ArrayPP.transfer(myStrings, new CSToMyCSIFactoryDelegate());

// ...

ArrayPP有:

public class ArrayPP<T> implements Iterable<T> {
    // ...
    // Iterable implementations
    // ...

    public static <From, To> ArrayPP<To> transfer(Iterable<From> origin, FactoryDelegate<From, To> factory)
    {
        ArrayPP<To> ret = new ArrayPP<>();
        for (From f : origin) {
            ret.add(factory.makeFromFactory(f));
        }
        return ret;
    }

    // ...
}

FactoryDelegate有:

public interface FactoryDelegate<From, To> extends Factory<To> {
    public To makeFromFactory(From basis);
}

Factory有:

public interface Factory<To> {
    public To makeFromFactory();
}

CSToMyCSIFactoryDelegate有:

public class CSToMyCSIFactoryDelegate implements FactoryDelegate<CharSequence, MyCharSequenceImpl> {
    @Override
    public MyCharSequenceImpl makeFromFactory(CharSequence basis) {
        // MyCharSequenceImpl has a constructor that converts any CharSequence
        // into a new MyCharSequenceImpl.
        return new MyCharSequenceImpl(basis);
    }

    @Override
    public MyCharSequenceImpl makeFromFactory() {
        return makeFromFactory("");
    }
}

这是我得到的错误:

error: method transfer in class ArrayPP<T> cannot be applied to given types;
        myCSI = ArrayPP.transfer(myStrings, new CSToMyCSIFactoryDelegate());
                       ^
  required: Iterable<From>,FactoryDelegate<From,To>
  found: ArrayPP<String>,TransferTest.CSToMyCSIFactoryDelegate
  reason: inferred type does not conform to equality constraint(s)
    inferred: CharSequence
    equality constraints(s): CharSequence,String
  where From,To,T are type-variables:
    From extends Object declared in method <From,To>transfer(Iterable<From>,FactoryDelegate<From,To>)
    To extends Object declared in method <From,To>transfer(Iterable<From>,FactoryDelegate<From,To>)
    T extends Object declared in class ArrayPP

对我来说,这个错误最令人困惑的部分是它说inferred type does not conform to equality constraint(s),然后推断的类型是CharSequence,等式约束是CharSequence,String ...... < / p>

为什么我会收到此错误,如何修复我的代码?

2 个答案:

答案 0 :(得分:3)

看来你需要的是

public static <From, To> ArrayPP<To> transfer(
    //       vvvvvvvvv
    Iterable<? extends From> origin,
    FactoryDelegate<From, To> factory
)

因为你只关心origin在迭代它时产生From,而不是它必然是Iterable<From>

另见

答案 1 :(得分:2)

From必须是来自第一个参数的String,并且必须是来自第二个参数的CharSequenceStringCharSequence的事实与您描述它们的方式无关。试试Iterable<? extends From>可能是?