问题
I need to build a set of input for different workflows using 1 or more outputs from previous workflows, when i do this
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;
}
I am getting and error and Netbeans tip fix gives me this. What am i doing wrong here ?
@Override
public SwfInput buildInput(SwfOutput... output) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
this is the exact error
method does not override or implement a method from a supertype
How can i avoid casting here ?
@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;
}
回答1:
buildInput(SwfOutput swfEventInitOutput)
and
buildInput(SwfOutput... swfEventInitOutput)
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:
swfEventInitOutput element = swfEventInitOutput.length > 0 ? swfEventInitOutput[0] : null;
if(element != null) { ... }
Another way is to iterate over the array and perform such actions for an each element:
for (swfEventInitOutput element : swfEventInitOutput) { ... }
In addition, I would suggest you specify generic types when you're implementing the InputBuilder
interface. It helps you to avoid castings (which you did) inside overridden methods.
A positive side here is that you used bounded generics types, it has prevented from Object...
(or Object[]
).
来源:https://stackoverflow.com/questions/39768434/passing-1-to-many-parameters-of-same-object-type