Java中的函数指针

JGC*_*JGC 2 java wicket

我正在用java编写一个在wicket中的Login页面,并希望尽可能地将其编写为通用,因此我需要将一个在C++中作为函数指针着名的函数传递给类.这堂课是:

class LoginForm extends Form
{

    public LoginForm(String id,TextField username,TextField password,WebResponse webResponse)
    {
        super(id);
    }

    @Override
    public void onSubmit()
    {
        String password = Login.this.getPassword();
        String userId = Login.this.getUserId();
        String role = authenticate(userId, password);
        if (role != null)
        {
            if (Login.this.getSave())
            {
                utilities.CreateCookie("Username", userId, false, 1209600, (WebResponse) getRequestCycle().getResponse());
                utilities.CreateCookie("Password", password, false, 1209600, (WebResponse) getRequestCycle().getResponse());
            }
            User loggedInUser = new User(userId, role);
            WiaSession session = (WiaSession) getSession();
            session.setUser(loggedInUser);
            if (!continueToOriginalDestination())
            {
                setResponsePage(UserHome.class);
            }
        }
        else
        {
            wrongUserPass.setVisible(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

身份验证是该功能我该怎么办?

Hen*_*rik 9

只需传递一个定义authenticate方法的接口.

void DoSomething(IAuthenticationProvider authProvider) {
    // ...
    authProvider.authenticate();
}
Run Code Online (Sandbox Code Playgroud)