将1传递给相同对象类型的许多参数 [英] Passing 1 to many parameters of same object type
问题描述
我需要为使用以前工作流的1个或多个输出的不同工作流建立一组输入,当我这样做时
public interface InputBuilder< I扩展了SwfInput,O扩展了SwfOutput> {
public I buildInput(O ... output);
$ b public class SwfAuthorisationInputBuilder实现了InputBuilder {
$ b $ @Override
public SwfAuthorisationInput buildInput(SwfOutput swfEventInitOutput){
SwfEventInitOutput输出=(SwfEventInitOutput)swfEventInitOutput;
SwfAuthorisationInput authorisationInput = oddFactory.newInstance(SwfAuthorisationInput.class);
authorisationInput.setNetworkEvent(output.getNetworkEvent());
返回authorisationInput;
}
我收到并发现错误,Netbeans tip fix给了我这个。我在这里做错了什么?
@Override
public SwfInput buildInput(SwfOutput ... output){
throw new UnsupportedOperationException(还不支持。); //要更改生成方法的主体,请选择工具|模板。
}
这是确切的错误
方法不会覆盖或实现超类型的方法
我如何避免投射在这里?
$ b $ pre $ @Override
public SwfAuthorisationInput buildInput(SwfOutput ... output){
SwfEventInitOutput swfEventInitOutput = (SwfEventInitOutput)输出[0];
SwfLocationDetectionOutput swfLocationDetectionOutput =(SwfLocationDetectionOutput)output [1];
SwfAuthorisationInput authorisationInput = oddFactory.newInstance(SwfAuthorisationInput.class);
authorisationInput.setNetworkEvent(swfEventInitOutput.getNetworkEvent());
返回authorisationInput;
}
buildInput(SwfOutput swfEventInitOutput)
和
buildInput(SwfOutput ... swfEventInitOutput)
是不同的方法签名(方法名称和Java中的参数类型)。如果您想重写一个方法,您必须 *指定来自父类的签名。 正如我所看到的,你只需要这个数组的一个元素。如果是这样,你可以在数组之前从数组中检出数组的大小: 另一种方法是迭代(swfEventInitOutput元素:swfEventInitOutput){...} $ b在数组上执行这些操作: 另外,我建议你指定泛型类型重新实现 这里的一个积极方面是您使用了有界的泛型类型,它阻止了 I need to build a set of input for different workflows using 1 or more outputs from previous workflows, when i do this I am getting and error and Netbeans tip fix gives me this. What am i doing wrong here ? this is the exact error method does not override or implement a method from a supertype How can i avoid casting here ?
and are the different method signatures (the method's name and the parameter types in Java). If you want to override a method, you have to exactly * specify a signature from the parent class. As I can see, you need only one element of this array. If so, you could pull out it from the array checking the array's size before: Another way is to iterate over the array and perform such actions for an each element:
In addition, I would suggest you specify generic types when you're implementing the A positive side here is that you used bounded generics types, it has prevented from 这篇关于将1传递给相同对象类型的许多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
swfEventInitOutput element = swfEventInitOutput.length> 0? swfEventInitOutput [0]:null;
if(element!= null){...}
$ b
InputBuilder
接口。它可以帮助您避免在重写的方法中出现铸件(您所做的)。
Object ...
(或 Object []
)。public interface InputBuilder<I extends SwfInput, O extends SwfOutput> {
public I buildInput(O... output);
}
public class SwfAuthorisationInputBuilder implements InputBuilder {
@Override
public SwfAuthorisationInput buildInput(SwfOutput swfEventInitOutput) {
SwfEventInitOutput output = (SwfEventInitOutput ) swfEventInitOutput;
SwfAuthorisationInput authorisationInput = oddFactory.newInstance(SwfAuthorisationInput.class);
authorisationInput.setNetworkEvent(output.getNetworkEvent());
return authorisationInput;
}
@Override
public SwfInput buildInput(SwfOutput... output) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public SwfAuthorisationInput buildInput(SwfOutput... output) {
SwfEventInitOutput swfEventInitOutput = (SwfEventInitOutput ) output[0];
SwfLocationDetectionOutput swfLocationDetectionOutput = (SwfLocationDetectionOutput) output[1];
SwfAuthorisationInput authorisationInput = oddFactory.newInstance(SwfAuthorisationInput.class);
authorisationInput.setNetworkEvent(swfEventInitOutput.getNetworkEvent());
return authorisationInput;
}
buildInput(SwfOutput swfEventInitOutput)
buildInput(SwfOutput... swfEventInitOutput)
swfEventInitOutput element = swfEventInitOutput.length > 0 ? swfEventInitOutput[0] : null;
if(element != null) { ... }
for (swfEventInitOutput element : swfEventInitOutput) { ... }
InputBuilder
interface. It helps you to avoid castings (which you did) inside overridden methods.Object...
(or Object[]
).