简体   繁体   English

如何避免实现所有方法

[英]How to avoid to implement all methods

I'm using an interface for a callback that I use in a lot of classes, so when I implement this callback I need to override all methods, but I only used 1 or 2 of then from class.我正在使用我在很多类中使用的回调接口,所以当我实现这个回调时,我需要覆盖所有方法,但我只使用了类中的 1 或 2 个。 How I can avoid this?我怎样才能避免这种情况?

This is the callback:这是回调:

public interface DatabaseCallback {

    void onContactPhotoSaved();

    void onContactPhotoUpdated();

    void onContactPhotoDeleted();

    void onContactPhotoFounded(ContactInfo item);

    void onDataNotAvailable();

    void onErrorDuringProcess();

    void onContactPhotoLoaded(List<ContactInfo> users);

}

EDIT:编辑:

This is possible??这个有可能??

public class DatabaseManagerCallback {

public interface ContactInfoInsertCallback{
    void onContactPhotoSaved();
    void onErrorOcurred();
}

public interface ContactInfoUpdateCallback{
    void onContactPhotoUpdated();
    void onErrorOcurred();
}

public interface ContactInfoDeleteCallback{
    void onContactPhotoDeleted();
    void onErrorOcurred();
}

public interface ContactInfoFoundedCallback{
    void onContactPhotoFounded(ContactInfo item);
    void onErrorOcurred();
}

} }

Write a class that implements all the functions but has an empty body for each.编写一个实现所有功能的类,但每个功能都有一个空的主体。 Then extend your classes from that, so they inherit the empty body functions.然后从中扩展您的类,以便它们继承空的主体函数。

If you're the owner of the interface class you can also consider giving them a default do nothing implementation.如果您是接口类的所有者,您还可以考虑给他们一个默认的什么都不做的实现。 But if its a library class that won't work.但是如果它是一个不起作用的库类。

There are only two things you can do with an interface implementation in Java language:对于 Java 语言中的接口实现,您只能做两件事:

  • Implement all methods (as you say).实现所有方法(如您所说)。
  • Implement only a few methods and declare the class abstract (and, therefore, you cannot instance it).只实现几个方法并声明类抽象(因此,您不能实例化它)。

Depending on what you want to do, you can create a class which implements all the methods with some kind of default behavior, and then override the methods you need to define in each case.根据您想要做什么,您可以创建一个类,该类实现具有某种默认行为的所有方法,然后覆盖您需要在每种情况下定义的方法。

You can create a class which implements all the methods and then inherit from it:您可以创建一个实现所有方法的类,然后从中继承:


public class MyDatabaseCallback implements DatabaseCallback{

    @Override void onContactPhotoSaved(){}

    @Override void onContactPhotoUpdated(){}

    @Override void onContactPhotoDeleted(){}

    @Override void onContactPhotoFounded(ContactInfo item){}

    @Override void onDataNotAvailable(){}

    @Override void onErrorDuringProcess(){}

    @Override void onContactPhotoLoaded(List<ContactInfo> users){}

}

Now, all you have to do is inherit from MyDatabaseCallback :现在,您所要做的就是从MyDatabaseCallback继承:

public class MyCustomDatabaseCallback extends MyDatabaseCallback {
    @Override void onDataNotAvailable(){
        // TODO
    }
}

You can add default implementations in your interface, so you won't need to override all the methods:您可以在接口中添加默认实现,因此您无需覆盖所有方法:

public interface DatabaseCallback {

    default void onContactPhotoSaved() {}

    default void onContactPhotoUpdated() {}

    default void onContactPhotoDeleted() {}

    default void onContactPhotoFounded(ContactInfo item) {}

    default void onDataNotAvailable() {}

    default void onErrorDuringProcess() {}

    default void onContactPhotoLoaded(List<ContactInfo> users) {}

}

This will allow you to extend another class with your implementation if needed.如果需要,这将允许您使用您的实现扩展另一个类。

If the interface comes from a library and you're not able to modify it, you can create an intermediate interface that extends the first one and adds the default implementations.如果接口来自库并且您无法修改它,您可以创建一个中间接口来扩展第一个接口并添加默认实现。 Then you implement your interface.然后你实现你的接口。

public interface MyDatabaseCallback extends DatabaseCallback {

    @Override default void onContactPhotoSaved() {}

    @Override default void onContactPhotoUpdated() {}

    @Override default void onContactPhotoDeleted() {}

    @Override default void onContactPhotoFounded(ContactInfo item) {}

    @Override default void onDataNotAvailable() {}

    @Override default void onErrorDuringProcess() {}

    @Override default void onContactPhotoLoaded(List<ContactInfo> users) {}

}

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

相关问题 如何避免激活类中的所有方法? - How can I avoid activating all the methods on a class? 如何避免需要将相同的参数传递给所有方法? - How to avoid the need to pass the same parameter to all methods? 如何获得接口必须实现的所有方法? - How can I get all the methods which have to implement of an interface? 如何在eclipse中为所有子类实现未实现的方法 - How to implement unimplemented methods to all child classes in eclipse 如何实现一个没有定义所有抽象方法的类? - How to implement a class that doesn't define all the abstract methods? 如何实现 REST 类以在单个方法中处理所有 HTTP 方法 - How to implement REST class to handle all HTTP methods in a single method 如何避免为未实现公共接口的相同库方法编写许多包装? - How to avoid writing many wrappers for identical library methods that do not implement a common interface? 避免覆盖Java中的所有抽象方法 - Avoid overriding all abstract methods in Java 如何实现自旋锁以避免阻塞 - How to implement a spinlock to avoid blocking 在WindowListener中,如何在使用WindowClosing()的单个事件时避免所有方法声明? - In WindowListener,how to avoid all methods declaration while using a single eventof WindowClosing()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM