[英]How to properly override Runnable.run(), so it can take parameters?
I am currently working on an android project and came across the situation, that I have to pass a function as parameter, so I browsed StackOverflow and tried out solution, to encapsulate the function with a Runnable
. 我目前正在处理一个android项目,遇到了这种情况,我必须将一个函数作为参数传递,因此我浏览了StackOverflow并尝试了解决方案,以将函数封装为
Runnable
。
The problem I'm now facing is, that the function I want to pass, expects parameters and Runnable.run()
doesn't take any. 我现在面临的问题是,我要传递的函数需要参数,而
Runnable.run()
却不接受任何函数。 So I'm currently trying to extend Runnable
and override run()
to let it accept parameters, which I can then pass to the actual function. 因此,我目前正在尝试扩展
Runnable
并覆盖run()
以使其接受参数,然后将其传递给实际函数。
Now I'm kinda unsure, as run()
is used in Thread
on how I can override it, without knocking out Thread
. 现在,我有点不确定,因为
Thread
使用了run()
来介绍如何覆盖它,而不用淘汰Thread
。 I don't use any other Thread
methods so far. 到目前为止,我没有使用任何其他
Thread
方法。 Any suggested approach? 有什么建议的方法吗?
EDIT 编辑
The target is, to call these custom Runnables inside a listener method like so: 目标是在侦听器方法中调用这些自定义Runnable,如下所示:
public void startRequest(RequestOperation operation, final Runnable onSuccess, final Runnable onError, final Runnable onFinished) {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
onSuccess.run();
onFinished.run();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onError.run();
onFinished.run();
}
});
queue.add(request);
}
In best case, onSuccess
is able get the response
object passed, as onError
gets the error
. 最好的情况是,
onSuccess
能够传递response
对象,因为onError
会获取error
。
Why force Runnable
into being something it's not designed to be? 为什么要强迫
Runnable
成为并非设计成的东西? Simply have your own interface that has the signature you require. 只需拥有自己的接口即可,该接口具有所需的签名。 Eg
例如
public interface Callback<T> {
void run(T parameter);
}
public void startRequest(RequestOperation operation, final Callback<JSONObject> onSuccess, final Callback<VolleyError> onError, final Runnable onFinished) {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
onSuccess.run(response);
onFinished.run();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onError.run(error);
onFinished.run();
}
});
queue.add(request);
}
Or even better (if you ask me): 甚至更好(如果您问我):
public interface Callback {
void onSucces(JSONObject response);
void onError(VolleyError error);
void onFinished();
}
public void startRequest(RequestOperation operation, final Callback callback) {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response);
callback.onFinished();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onError(error);
callback.onFinished();
}
});
queue.add(request);
}
You can also make Callback
generic again, if you need to: 如果您需要执行以下操作,也可以使
Callback
再次通用:
public interface Callback<R, E> {
void onSucces(R response);
void onError(E error);
void onFinished();
}
public void startRequest(RequestOperation operation, final Callback<JSONObject, VolleyError> callback) {...}
To use: 使用方法:
public class SimpleCallback implements Callback {
public void onSucces(JSONObject response) {
doSomethingWithResponse(response);
}
public void onError(VolleyError error) {
doSomethingWithError(error);
}
void onFinished() {
logFinishTime();
}
}
startRequest(operation, new SimpleCallback());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.