如何在java中传递变量参数/一个包含变量参数的对象?

时间:2014-04-10 18:09:16

标签: java argument-passing variadic-functions

我要做的是创建一个可用于实现连接类的接口。我应该能够将它用于LDAP,OpenId等不同的身份验证;所以我想传递用户名,密码和可变数量的参数。我该怎么做..我试过这个。我是朝着正确的方向前进的吗?如果是这样,我如何初始化对象以保存变量参数。新手到java。非常感谢帮助。谢谢!

package com.cerner.jira.plugins.esig.servicemanager;
import javax.naming.AuthenticationException;

public interface AuthenticationServiceManager {

   /**
    * Creates the Connection for the specific user logging in, and binds the
    * user's credentials to  object.
    * 
    * @param userName
    *            The user name to authenticate .
    * @param password
    *            The user's password to check .
    * @param args
    *            is an object that holds variable arguments which can be used
    *            to authenticate using both LDAP and OpenId
    * @return boolean The connection status showing whether the user has been
    *         successfully authenticated or not.
    * @throws AuthenticationException
    *             If there is an error authenticating with the passed
    *             parameters
    **/

    boolean authenticate(String username, String password, Object... args)
        throws AuthenticationException;

    /**
    * Disconnects the connection.
    */
    void disconnect();
}

1 个答案:

答案 0 :(得分:0)

看起来你已经得到了它。你这样称呼它:

authenticate(username, password, someOtherArgument, yetAnotherArgument, stillAnotherArgument);

然后在您的方法中,args[0]将包含someOtherArgumentargs[1]将包含yetAnotherArgumentargs[2]将包含stillAnotherArgument