switch 语句调用 AsyncTask 中的所有事例,即使变量不匹配



我用switch语句创建了一个AsyncTask。这里称为测试和医学案例。非常奇怪的行为

BackendAsync backendAsync = new BackendAsync();
backendAsync.execute("Test");
public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
}

}
return null;
}
}

结果:

D/Test: doInBackground:

称为测试 D/Medicine: doInBackground: 称为医学

BackendAsync backendAsync = new BackendAsync();
backendAsync.execute("Test");
public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
}
break;
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
}
breakl

}
return null;
}
}

在每个案例后添加中断

public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
break;
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
break;
}

}
return null;
}
}

像这样使用它

case "Test": {
Log.d("Test", "doInBackground: Called Test");
break;
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
break;
}

相关内容

最新更新