传递变量参数时出错

时间:2014-04-10 21:02:08

标签: java parameter-passing argument-passing

我有一个界面

    import javax.naming.AuthenticationException;

    import com.unboundid.ldap.sdk.LDAPException;


    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, LDAPException;

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

}

实现此接口的类

    package com.cerner.jira.plugins.esig.servicemanagerimpl;

   import javax.naming.AuthenticationException;

    import com.cerner.jira.plugins.esig.servicemanager.AuthenticationServiceManager;
   import com.unboundid.ldap.sdk.LDAPException;

    public class AuthenticationManagerImpl implements AuthenticationServiceManager {

@Override
public boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException {

    return false;
}

@Override
public boolean authenticate(String username, String password, String url)
{
    return false;


}

@Override
public void disconnect() {
    // TODO Auto-generated method stub

}

}

我试图创建一个可用于实现连接类的接口。我应该能够将它用于LDAP,OpenId等不同的身份验证;所以我想传递用户名,密码(如果它的LDAP)和可变数量的参数(如果是OpenId)。我怎么做?我试过这个。这是投掷错误。如何初始化对象以保存变量参数?

错误:AuthenticationManagerImpl类型的方法authenticate(String,String)必须覆盖或实现超类型方法

2 个答案:

答案 0 :(得分:0)

有几种可能性。

一种方法是通过使用不同的参数集多次声明该方法来重载接口中的方法。

另一种方法是将参数列为可变长度数据结构,例如数组。命令行界面使用public static void main(String[] args)执行此操作,您可以传递多个命令行参数。您可以按如下方式执行此操作:

boolean authenticate(String username, String password, Object[] args)
    throws AuthenticationException, LDAPException;

答案 1 :(得分:0)

@Override
public boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException {

    return false;
}

@Override
public boolean authenticate(String username, String password, String url) {
    return false;
}

AuthenticationServiceManager接口中没有接收String, String个参数或String, String, String参数的方法。相反,您有一个接收varargs的authenticate方法,因此,在实现AuthenticationServiceManager接口的类中,您应该将该方法实现为:

@Override
public boolean authenticate(String username, String password, Object ... args) {
    //handle args as you want/need
    //if you don't need it for this specific case, then do nothing with the variable
    //and yes, it is an ugly design =\
    return false;
}

另一个设计选项是AuthenticationServiceManager接口与boolean authenticate(String username, String password)方法和其他方法,您可以根据需要添加更多参数:

public interface AuthenticationServiceManager {
    boolean authenticate(String username, String password)
        throws AuthenticationException, LDAPException;
    boolean authenticate(String username, String password, String url)
        throws AuthenticationException, LDAPException;
    boolean authenticate(String username, String password, String url, String anotherParam)
        throws AuthenticationException, LDAPException;
    //and on and on...
    boolean authenticate(String username, String password, Object... args)
        throws AuthenticationException, LDAPException;

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