Java 需要一些帮助来理解在第二行代码中做了什么? public void addToRequestQueue(请求请求,字符串标记){ 请求setTag(TextUtils.isEmpty(tag)?tag:tag; getRequestQueue().add(请求); }

Java 需要一些帮助来理解在第二行代码中做了什么? public void addToRequestQueue(请求请求,字符串标记){ 请求setTag(TextUtils.isEmpty(tag)?tag:tag; getRequestQueue().add(请求); },java,string,ternary-operator,Java,String,Ternary Operator,第二行是什么?我不明白这是什么(标签)?标签:标签 这是完整的代码 public <T> void addToRequestQueue(Request<T> req, String tag) { req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getRequestQueue().add(req); } { 导入android.app.Application; 导入android.text.TextUtil

第二行是什么?我不明白这是什么(标签)?标签:标签

这是完整的代码

 public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}
{
导入android.app.Application;
导入android.text.TextUtils;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.toolbox.volley;
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class.getSimpleName();
私有请求队列mRequestQueue;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共无效addToRequestQueue(请求请求,字符串标记){
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}

下面是完整的代码,请告诉我,如果可以的话,请解释一下,在该方法的第二行

标记
中所做的事情必须是某种类型的常量,表示标记的默认值,不管它是什么-类似这样的:

{
import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}
公共静态最终字符串标记=”;
这里的想法是使用作为第二个参数传递的值标记您的请求,即
字符串标记
?:
用于执行检查,而不使用
if
/
else
语句


当调用
addToRequestQueue
时传递的
标记
字符串恰好为空时,该方法使用
标记的值

转换第二行

public static final String TAG = "<default-tag>";

我们怎么知道?这是你的代码。看起来像类请求的某个实例变量。如果它足够通用,请提供完整的上下文。你是在问?:运算符如何工作?是的,运算符如何工作,以及该方法第二行的代码是什么?表达式“b?v1:v2”计算布尔值“b”如果为true,则表达式的值为v1,否则它的值为v2。类似于“int问号(布尔b,int v1,int v2){if(b)return v1;return v2;}”但它并不局限于int,v1/v2可以是任何类型。感谢John…感谢这样一个概念,非常感谢您的知识。请补充这是三元或内联if运算符。还有一个链接解释这一点。
public static final String TAG = AppController.class.getSimpleName();

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag("is tag empty?" yes, so use `TAG` : no, so use `tag`);
    getRequestQueue().add(req);
} 
if(tag.isEmpty()){
  res.setTag(TAG)

} else {
  res.setTag(tag)

}