在java中正确使用接口
具有此接口
public interface BookService {
List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet);
}
当在其实现中不使用变量 int childLimitPages 时,具有 2 种实现。
public class AdultBookList implements BookService {
// This won't work since there is missing variable, I am asking if there is other way to provide this ask
List<Book> search(int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
.....
}
}
public class ChildrenBookList implements BookService {
List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
.....
}
}
我的问题我应该如何使用它?我应该有2个接口吗?我可以有默认变量吗?
Having this interface
public interface BookService {
List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet);
}
Having 2 implementations when one does not use variable int childLimitPages in its implementation.
public class AdultBookList implements BookService {
// This won't work since there is missing variable, I am asking if there is other way to provide this ask
List<Book> search(int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
.....
}
}
public class ChildrenBookList implements BookService {
List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
.....
}
}
My question how should I use it? Should I have 2 interface? Could I have default variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您实现接口时,您需要定义具有与接口中声明的完全相同的签名的所有方法。
因此该类
不是接口
BookService
的有效实现,因为方法 search 具有不同的签名。您可以通过不同的方式解决它。
第一种方法是重载接口中的方法搜索,并提供不带 childLimitPages 参数的方法。在实现中,如果您使用 childLimitPages 调用该方法,则可以在 AdultBookList 中引发异常。但我建议你用第二种更优雅的方式来解决它。
第二种方法是将所有参数替换为包装它们的对象。在此对象中,您可以定义默认值,因此当未显式设置该值时,它将采用默认值,如下所示:
When you implement an interface you need to define all methods with exactly the same signature declared in the interface.
So the class
is not a valid implementation of the interface
BookService
because the method search has a different signature.You can solve it in different ways.
First way is to overload the method search in the interface and provide also the method without the childLimitPages parameter. In the implementations you can throw an exception in the AdultBookList if you are calling the method with the childLimitPages. But I suggest you a more elegant way to solve it in the second way.
Second way is to replace all the parameters with an object wrapping them. In this object you can define default values, so when it is not explicitly set the value it takes the default value, so something like:
您可以将第一个参数定义为
Optional
并断言在 AdultBookService 中存在Optional 中的值。我喜欢我的 API 在末尾有可选参数。并将调用重定向到解析选项的内部计算。就像这样:
You could define the first argument as
Optional<Integer>
and assert that in the AdultBookService the value in the Optional is present.I like my APIs to have optional arguments at the end. And to redirect the calls to an internal computation where the optionals are resolved. Like so:
在您的界面上定义多个方法。
您有两种不同的行为,因此您必须提供两种不同的方法。提供
searchForPageCount
和searchByNumberOfBooks
,而不是一种search
方法。Define multiple methods on your interface.
You have two different behaviors, so you must offer two different methods. Instead of one
search
method, offersearchForPageCount
andsearchByNumberOfBooks
.