简体   繁体   English

如何在Java中传递变量参数/持有变量参数的对象?

[英]How to pass variable arguments/ an object holding variable arguments in java?

What I'm trying to do is to create an interface that can be used to implement a connection class. 我正在尝试做的是创建一个可用于实现连接类的接口。 I should be able to use this for different authentication like LDAP, OpenId etc; 我应该能够将此用于不同的身份验证,例如LDAP,OpenId等; So I want to pass username,password and variable number of arguments. 所以我想传递用户名,密码和可变数量的参数。 How do I do that.. I tried this. 我该怎么做。 Am I going in the right direction.? 我朝着正确的方向前进吗? If so, how do I initialize the object to hold variable parameters.? 如果是这样,如何初始化对象以容纳变量参数。 Newbie to java. Java新手。 would greatly appreciate the help. 非常感谢您的帮助。 Thanks! 谢谢!

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

Looks like you've got it. 看来您已掌握。 You call it like this: 您这样称呼它:

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

And then inside your method, args[0] will contain someOtherArgument , args[1] will contain yetAnotherArgument and args[2] will contain stillAnotherArgument . 然后在您的方法内部, args[0]将包含someOtherArgumentargs[1]将包含yetAnotherArgumentargs[2]将包含stillAnotherArgument

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

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