如何将构造函数中的嵌套方法分离为单独的方法

How do you separate a nested method in a constructor to a separate method

这更像是一种风格的东西,也是一种自学的东西,但是凌空代码中是监听器,我在网上找到的所有代码都只是在构造函数中嵌套了一个重写方法。不一定是我从 C# 背景习惯的东西。其实 lambda 不太好,匿名方法也不是。

我不知道从哪里开始,因为现在对我来说似乎不直观。但我想将嵌套方法分离到它们各自的方法中。或者,如果这是唯一需要的部分,甚至可能只是重写的方法。

    final EditText textView = (EditText) findViewById(R.id.editText);

    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://myURL";

    JSONObject postparams = new JSONObject();
    postparams.put("city", "london");
    postparams.put("name", "bob");

    // Request a string response from the provided URL.
    JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    textView.setText("Success: "+ response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText(String.valueOf(error.networkResponse.statusCode));
                }
            }
    );
    // Add the request to the RequestQueue.
    queue.add(postRequest);

我想要的是在同一个 class 中的 2 个 Response 参数中命名一个方法,或者这本身可以进行覆盖。可能吗?有点像。

...
JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams, myResponseListener(JSONObject response), myErrorResponseListener(VolleyError error));
// Add the request to the RequestQueue.
queue.add(postRequest);
}

public void myResponseListener(JSONObject response){
     textView.setText("Success: "+ response.toString());
}
public void myErrorResponseListener(VolleyError error){
    textView.setText(String.valueOf(error.networkResponse.statusCode));
}

有这样的简单方法吗?

编辑:尝试 linucksrox 的回答,令我惊讶的是,以下实际上是独立的方法......没有 public(访问修饰符)或 void(return 类型)??

    Response.Listener<JSONObject> myListener(JSONObject response)
    {
        return new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                textView.setText("Success: "+ response.toString());
            }
        };
    }

但是当我尝试将 myListener 作为第四个参数插入时,它会抱怨这些参数。

myListener() no work, 
myListener(JSONOBject response) no work

错误部分参数也是如此。

您应该可以这样做:

    Response.Listener<JSONObject> myListener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            textView.setText("Success: "+ response.toString());
        }
    };

    Response.ErrorListener myErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText(String.valueOf(error.networkResponse.statusCode));
        }
    };

    // Request a string response from the provided URL.
    JsonObjectRequest  postRequest = new JsonObjectRequest(Request.Method.POST, url, postparams,
            myListener, myErrorListener);