我在 ForumService 类中有一个 ForumApi 接口(interface)。我在这里使用 Retrofit 和 Rxjava。通常对于 Retrofit,我们实现一个接口(interface),但在下面的代码中我看到了类内部的接口(interface)。这有什么具体原因吗?
public class ForumService {
public interface ForumApi {
@GET("/posts")
public Observable<List<Post>>
getPosts();
@GET("/posts/{id}")
public Observable<Post>
getPost(@Path("id") int postId);
@GET("/comments")
public Observable<List<Comment>>
getComments(@Query("postId") int postId);
@POST("/posts")
public Observable<Post>
postPost(Post post);
}
}
最佳答案
如果接口(interface) A
旨在由特定类/接口(interface) B
使用,那么通过声明 A
将它们绑定(bind)在一起是有意义的> 在 B
内。
另一个这样的例子是在 Map
接口(interface)中声明的接口(interface) Map.Entry
(例如,你不应该使用 Map.Entry
在 List
中)。
关于java - 为什么我们需要在类内部声明一个接口(interface)?我们这样做有什么好处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43829318/