将类嵌套在接口中是个好主意吗?
java中的接口中是否可以有一个内部类???
Is it possible to have an inner class inside the interface in java ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
java中的接口中是否可以有一个内部类???
Is it possible to have an inner class inside the interface in java ???
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
你可以。但这是 O'Reilly 对此的说法:
嵌套类在接口中?
Java 支持接口中嵌套类的概念。语法和动态工作就像类中声明的嵌套类一样。然而,声明一个嵌套在接口中的类将是非常糟糕的编程。接口是概念的抽象,而不是概念的实现。因此,接口中不应包含实现细节。请记住,仅仅因为你可以用锯子砍掉你的手,并不意味着这是一个特别好的主意。
也就是说,我可以看到嵌套在接口中的静态实用程序类的参数。尽管为什么它需要嵌套在接口中而不是作为一个独立的类完全是主观的。
You can. But here's what O'Reilly says about it:
Nested Classes in Interfaces?
Java supports the concept of nested classes in interfaces. The syntax and dynamics work just like nested classes declared in a class. However, declaring a class nested inside an interface would be extremely bad programming. An interface is an abstraction of a concept, not an implementation of one. Therefore, implementation details should be left out of interfaces. Remember, just because you can cut off your hand with a saw doesn't mean that it's a particularly good idea.
That said, I could see an argument for a static utility class nested into an interface. Though why it would need to be nested into the interface instead of being a stand-alone class is completely subjective.
我同意这种情况通常很少见,但当接口方法需要返回多条信息时,我确实喜欢在服务接口中使用内部类,因为它实际上是契约的一部分而不是实现。例如:
显然这也可以在一个单独的类中完成,但对我来说,感觉就像我将接口定义的整个 API 保留在一个位置,而不是分散开来。
I agree that this should be generally rare, but I do like to use inner classes in interfaces for services when the interface method needs to return multiple pieces of information, as it's really part of the contract and not the implementation. For example:
Obviously this could be done in a separate class as well, but to me it feels like I'm keeping the whole API defined by the interface in one spot, rather than spread out.
是的,这是可能的,但这不是常见的做法。
Yes, it is possible but it is not common practice.
不直接回答您的问题,但在相关说明中,您也可以将一个接口嵌套在另一个接口中。这是可以接受的,特别是如果您想提供观点的话。 Java 的集合类可以执行此操作,例如
Map.java
中的Map.Entry
视图:这是可以接受的,因为您没有将实现细节混合到界面中。您只是指定另一份合同。
Doesn't answer your question directly, but on a related note you can also nest an interface inside another interface. This is acceptable, especially if you want to provide views. Java's collection classes do this, for example
Map.java
in the case of theMap.Entry
view:This is acceptable because you're not mixing implementation details into your interface. You're only specifying another contract.
是的。直接来自语言规范:
和(粗体是我的):
Yes. Straight from the language spec:
And (boldface mine):
我发现非常有用的一个用例是,如果您有一个创建接口实例的构建器。如果构建器是接口的静态成员,则可以创建如下实例:
One use case for this that I find quite useful is if you have a builder that creates an instance of the Interface. If the builder is a static member of the Interface, you can create an instance like this:
这是合法的,但我实际上只使用嵌套接口(如前所述)或嵌套枚举来做到这一点。例如:
It is legal, but I only really do it with nested interfaces (as already mentioned) or nested enums. For example: