问题
I wrote a simple weather app which goes fetches data from Openweathermap's API in Json format. I used an Aynctask to fetch the data, using ideas from online (as we all do!)
All works ok, but I am trying to get a better understanding of the callback implementation (like this one one at How to use callback or interface - Pass data from Asynctask to activity
In particular, I am trying to understand how is the interface is implemented without the keyword implements
anywhere?
The Async thread:
class MyTask extends AsyncTask {
public interface OnUpdateListener {
public void onUpdate(MyObject obj);
}
OnUpdateListener listener;
MyTask() {}
public void setUpdateListener(OnUpdateListener listener) {
this.listener = listener;
}
MyObject doInBackground() {
return obj;
}
onPostExecute(MyObject obj) {
if (listener != null) {
listener.onUpdate(obj);
}
}
}
In Mainactivity:
MyActivity extends Activity {
void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTask task = new MyTask();
task. setUpdateListener(new MyTask. OnUpdateListener() {
public void onUpdate(MyObject obj) {
.....
}
});
task.execute();
}
}
来源:https://stackoverflow.com/questions/61329291/understanding-how-is-this-interface-implemented-in-asynctask