简体   繁体   English

如何正确重写Runnable.run(),以便它可以接受参数?

[英]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.

相关问题 runnable.run()是不是错了? - Is it wrong to do runnable.run()? 显式调用Runnable.run - Explicit call of Runnable.run Runnable.run InterruptedException 中恢复的中断如何传播到调用方方法? - How does the restored interrupt in a Runnable.run InterruptedException propagate to the caller method? 为什么这个程序打印“你好”? (在 Java 中函数指针如何转换为 Runnable.run()) - Why does this program print "Hello"? (how is a function pointer translated to Runnable.run() in Java) 在JAVA Servlet中将Runnable.run()转换为Callable.call() - Convert Runnable.run() to Callable.call() in JAVA Servlet 从Java中的抽象Runnable.run()引发异常 - Throwing an exception from an abstract Runnable.run() in Java 从 SwingWorker 调用 done 方法时是否必须使用 Runnable.run - Is it mandatory to use Runnable.run while calling done method from SwingWorker Java CountDownLatch.await() 抛出异常“与 Runnable.run() 中的 throws 子句不兼容” - Java CountDownLatch.await() throws Exception "not compatible with throws clause in Runnable.run()" 如何将以下示例转换为实现Runnable接口并覆盖run方法 - How to convert below example into implements Runnable interface and override run method 如何解决Runnable中运行方法的编译器问题“方法不会从其超类@Override中覆盖方法”? - How to fix compiler issue “method does not override a method from its superclass @Override” for run method in Runnable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM