简体   繁体   English

泛型和varargs java

[英]Generics and varargs java

Below is my set up 以下是我的设置

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)}}

which in turn is results in java.lang.ClassCastException: [Lcom.test.MyInterface; 反过来又导致java.lang.ClassCastException:[Lcom.test.MyInterface; cannot be cast to [Lcom.test.Impl.MyInterfaceImpl; 无法转换为[Lcom.test.Impl.MyInterfaceImpl;

but the below works 但下面的工作

testfactory.getTest(Type type).someMethod(new MyInterfaceImpl[]{new MyInterfaceImpl()}) 

Not sure what is happening. 不确定发生了什么。 Help please 请帮忙

Ok .. the problem is within the design of your already existing code (which you can't change). 好的..问题在于您现有代码的设计(您无法更改)。 Having public interface Test<T extends MyInterface> and then public class TestImpl implements Test<MyInterfaceImpl> is wrong. public interface Test<T extends MyInterface>然后public class TestImpl implements Test<MyInterfaceImpl>是错误的。

TestImpl is implementing Test with MyInterfaceImpl whereas the original Test interface only expects an object that extends MyInterface and not implement it. TestImpl使用MyInterfaceImpl实现Test ,而原始的Test接口只需要一个extends MyInterface而不实现它的对象。

There will be a type confusion at runtime when executing the code. 执行代码时,运行时会出现类型混淆。 Not only does the following line throw a ClassCastException 以下行不仅抛出ClassCastException

test.someMethod(new MyInterfaceImpl()); 

but also test.someMethod(); 还有test.someMethod(); by itself throws an exception. 本身抛出异常。 So let's say if you factory called this method passing no argument, you would still get an exception as the original designed is flawed. 所以,假设你工厂调用这个方法不传递任何参数,你仍然会得到一个例外,因为原设计是有缺陷的。 In a normal situation test.someMethod(); 在正常情况下test.someMethod(); should not throw an exception to begin with. 不应该抛出异常开始。 You will need to talk to the appropriate party to put a fix to this serious issue. 您需要与相关方联系,以解决此严重问题。

Below is a sample solution: 以下是一个示例解决方案:

The method someMethod(MyInterface...) belongs to the raw type Test . 方法someMethod(MyInterface...)属于原始类型Test References to generic type Test<T> should be parameterized. 应参数化对泛型类型Test<T>引用。

This means you should Test<MyInterfaceImpl> test to avoid getting this error with only the new operator. 这意味着您应该Test<MyInterfaceImpl> test以避免仅使用new运算符获得此错误。

Test<MyInterfaceImpl> test
...
test.someMethod(new MyInterfaceImpl()); 

The above code will work with no problem. 上面的代码没有问题。

A better solution is in your TestImpl class do the following 更好的解决方案是在您的TestImpl类中执行以下操作

public class TestImpl implements Test<MyInterface>{...}

instead of 代替

public class TestImpl implements Test<MyInterfaceImpl>{...}

That way you don't need to explicitly parameterize your object instance (ie you could just do 这样您就不需要显式地参数化您的对象实例(即您可以这样做

Test test
...
test.someMethod(new MyInterfaceImpl());

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM