小编use*_*275的帖子

泛型和varargs java

以下是我的设置

public interface Test<T extends MyInterface> 
{
    someMethod(T... a)
}

public class TestImpl implements Test<MyInterfaceImpl> 
{
    someMethod(MyInterfaceImpl... a)
}

public class MyInterfaceImpl implements MyInterface {}

public someClass { @Autowired TestFactory testfactory 

......

// getting an error --  Type mismatch Can't assign non-array value to an array 
testfactory.getTest(Type type).someMethod(new MyInterfaceImpl()) 

}

public class TestFactoryImpl implements TestFactory { Test getTest(Type type) { return registry.get(type)}}
Run Code Online (Sandbox Code Playgroud)

反过来又导致java.lang.ClassCastException:[Lcom.test.MyInterface; 无法转换为[Lcom.test.Impl.MyInterfaceImpl;

但下面的工作

testfactory.getTest(Type type).someMethod(new MyInterfaceImpl[]{new MyInterfaceImpl()}) 
Run Code Online (Sandbox Code Playgroud)

不确定发生了什么.请帮忙

java generics interface variadic-functions

3
推荐指数
1
解决办法
631
查看次数

标签 统计

generics ×1

interface ×1

java ×1

variadic-functions ×1