[英]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 语言中的接口实现,您只能做两件事:
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.