Java中的函数指针
我正在用 java 在 wicket 中编写一个登录页面,并希望将其编写得尽可能通用,因此我需要将一个函数传递给类,该函数在 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);
}
}
}
验证该函数在哪里我应该做什么?
I'm writing a Login page in wicket by java and want to write it as general as possible so I need to pass to class a function which famous as Function Pointer in C++.
The class is:
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);
}
}
}
where authenticate is that function what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需传递一个定义
authenticate
方法的接口即可。Just pass an Interface which defines the
authenticate
method.您可以使用内部类
You can use inner class
在java中没有任何东西称为函数指针,而是函数对象。 在此您可以定义其方法对其他对象执行操作的对象。 例如,
这只不过是函数对象。 如果我们遵循代码接口规则,那么我们就可以定义接口并且也可以使用泛型。
现在原来的类可以实现这个接口了。 上面的例子只是Comparator接口例子的简单修改。 如果您不需要具体类,也可以使用匿名类。
希望能帮助到你。
In java there is nothing call function pointer rather it is function object. In this you can define object whose methods performs operation on the other objects. e.g.
This is nothing but function object. If we follow code to interface rule then we can define an interface and can use generics as well.
Now the original class can implement this interface. The above example is just a simple modification of Comparator interface example. If you don't want concrete class you can use anonymous class as well.
Hope it helps.