将1传递给相同对象类型的许多参数 [英] Passing 1 to many parameters of same object type

查看:98
本文介绍了将1传递给相同对象类型的许多参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为使用以前工作流的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中的参数类型)。如果您想重写一个方法,您必须 *指定来自父类的签名。
$ b




正如我所看到的,你只需要这个数组的一个元素。如果是这样,你可以在数组之前从数组中检出数组的大小:

  swfEventInitOutput element = swfEventInitOutput.length> 0? swfEventInitOutput [0]:null; 
if(element!= null){...}

另一种方法是迭代(swfEventInitOutput元素:swfEventInitOutput){...} $ b在数组上执行这些操作:

  $ b  






另外,我建议你指定泛型类型重新实现 InputBuilder 接口。它可以帮助您避免在重写的方法中出现铸件(您所做的)。



这里的一个积极方面是您使用了有界的泛型类型,它阻止了 Object ... (或 Object [] )。


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

解决方案

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[]).

这篇关于将1传递给相同对象类型的许多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆