【问题标题】:Error in passing variable arguments传递变量参数时出错
【发布时间】:2014-04-10 21:02:08
【问题描述】:

我有一个界面

    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) 必须重写或实现超类型方法

【问题讨论】:

  • “抛出错误。” 什么错误?总是,总是,总是包含这样的基本信息。为什么你会认为它是可选的?
  • 我认为您在这里有点重新发明轮子。你检查过这个吗? docs.spring.io/spring-security/site/docs/3.1.x/reference/…
  • 对此我很抱歉..这是错误“AuthenticationManagerImpl 类型的方法 authenticate(String, String) 必须覆盖或实现超类型方法”

标签: java parameter-passing argument-passing


【解决方案1】:

几种可能性。

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

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

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

【讨论】:

    【解决方案2】:
    @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;
    }
    

    AuthenticationServiceManagerinterface 中没有接收 String, String 参数或 String, String, String 参数的方法。相反,您有一个接收可变参数的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();
    }
    

    【讨论】:

    • 我明白这一点,但重点是我想让方法通用......以接受可变参数。这可能吗?
    • @user3438489 varargs 声明已经为您处理了这个问题。你还期待什么?
    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2013-06-12
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多