简体   繁体   English

默认接口实现

[英]Default interface implementation

In Android, when I use AsyncTask , I usually use interface method, so that I can implement it in activity : 在Android中,当我使用AsyncTask ,通常使用接口方法,以便可以在activity实现它:

 public class DownloadTask extends AsyncTask<String, Integer, Response> {

private Context context;
private DownloadTaskListener listener;
private List<MyObject> objectList;


public interface DownloadObjectsTaskListener {
    void startTask();
    void finishTask();
    void ObjectsDownloaded(List<Object> ObjectList);

    void withoutInternet();
}

public DownloadObjectsTask(Context context) {
    this.context = context;
}


public void setListener(DownloadObjectsTaskListener listener) {
    this.listener = listener;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    if (CommonUtils.isThereInternetConnection(context)) {
        listener.startTask();
    } else {
        listener.withoutInternet();
        cancel(true);
    }
}

protected Response doInBackground(String... urls) {
    RestApiObject api2 = new RestApiObject();
    return api2.getMyObjects();

}

protected void onPostExecute(Response results) {
    listener.finishTask();
    if (results != null) {
        if (results.body() != null) {
            ObjectsResponse request = (ObjectsResponse) results.body();
            ObjectList = request.getObjectsList();
            listener.ObjectsDownloaded(ObjectList);
        } else {
            ResponseBody jsonObject = results.errorBody();
            try {
                Log.d(DEBUG, jsonObject.string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        Log.d(DEBUG, "result is null");
    }

   }
}

And then In my Activity , I implement the interface, and define the method. 然后在Activity ,实现接口并定义方法。

The thing is for method such as withoutInternet() , the method definition will always be the same, so it will just be redundant code each time I redefine method implementation, even if I extract code in a method. 事情是针对诸如withoutInternet()之类的方法,方法定义将始终相同,因此,即使我在方法中提取代码,每次我重新定义方法实现时,它都将是冗余代码。

Is there a way to define just once withoutInternet() ? 有没有一种方法可以只定义一次withoutInternet()

Sure, you want an abstract class instead of an interface. 当然,您需要一个abstract类而不是一个interface. Like this: 像这样:

public abstract DefaultDownloadObjectTasksListener implements DownloadObjectsTaskListener{
    abstract void startTask();
    abstract void finishTask();
    abstract void ObjectsDownloaded(List<Object> ObjectList);

    void withoutInternet(){
      //some default implementation
    }
}

And then you can extend (versus implement ) the abstract class overriding the methods you want to provide functionality for. 然后,您可以extend (而不是implement )抽象类,以覆盖要为其提供功能的方法。 Because the first three methods are abstract you will be required to override them. 由于前三个方法是abstract您将需要重写它们。 However, because withoutInternet is not abstract, the default implementation will be used, unless you choose to override it. 但是,因为没有withoutInternet不是抽象的,所以将使用默认实现,除非您选择覆盖它。

Edit To address your issue: the solution then would be to make your DefaultDownloadObjectTasksListener extend AppCompatActivity and implement DownloadObjectsTaskListener and then extend DefaultDownloadObjectTasksListener from your activity. 编辑为了解决您的问题:解决方案将是使DefaultDownloadObjectTasksListener扩展AppCompatActivity并实现DownloadObjectsTaskListener ,然后从您的活动扩展DefaultDownloadObjectTasksListener

Or, really, what I would do is use an anonymous subclass as a listener if you really want a default implementation. 或者,实际上,如果您确实想要默认实现,那么我将使用匿名子类作为侦听器。

You can create a base activity that implements your interface, implement withoutInternet() , and declare the other methods as abstract. 您可以创建实现您的接口的基本活动,实现没有withoutInternet()实现,并将其他方法声明为抽象方法。 Your activities would then extend your activities from the base activity and you only need to implement the other methods. 然后,您的活动将从基本活动扩展您的活动,而您只需要实现其他方法即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM