将类嵌套在接口中是个好主意吗?

发布于 2024-09-12 17:31:02 字数 29 浏览 2 评论 0原文

java中的接口中是否可以有一个内部类???

Is it possible to have an inner class inside the interface in java ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沉鱼一梦 2024-09-19 17:31:02

你可以。但这是 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.

作死小能手 2024-09-19 17:31:02

我同意这种情况通常很少见,但当接口方法需要返回多条信息时,我确实喜欢在服务接口中使用内部类,因为它实际上是契约的一部分而不是实现。例如:

public interface ComplexOperationService {

    ComplexOperationResponse doComplexOperation( String param1, Object param2 );

    public static class ComplexOperationResponse {
        public int completionCode;
        public String completionMessage;
        public List<Object> data;
        // Or use private members & getters if you like...
    }

}

显然这也可以在一个单独的类中完成,但对我来说,感觉就像我将接口定义的整个 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:

public interface ComplexOperationService {

    ComplexOperationResponse doComplexOperation( String param1, Object param2 );

    public static class ComplexOperationResponse {
        public int completionCode;
        public String completionMessage;
        public List<Object> data;
        // Or use private members & getters if you like...
    }

}

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.

自控 2024-09-19 17:31:02

是的,这是可能的,但这不是常见的做法。

interface Test
{
    class Inner
    { }
}

class TestImpl implements Test
{
    public static void main(String[] arg)
    {
        Inner inner = new Inner();
    }
}

Yes, it is possible but it is not common practice.

interface Test
{
    class Inner
    { }
}

class TestImpl implements Test
{
    public static void main(String[] arg)
    {
        Inner inner = new Inner();
    }
}
酷到爆炸 2024-09-19 17:31:02

不直接回答您的问题,但在相关说明中,您也可以将一个接口嵌套在另一个接口中。这是可以接受的,特别是如果您想提供观点的话。 Java 的集合类可以执行此操作,例如 Map.java 中的 Map.Entry 视图:

public interface Map<K,V> {
   ...
   public static interface Entry<K,V> {
       ....
   }
}

这是可以接受的,因为您没有将实现细节混合到界面中。您只是指定另一份合同。

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 the Map.Entry view:

public interface Map<K,V> {
   ...
   public static interface Entry<K,V> {
       ....
   }
}

This is acceptable because you're not mixing implementation details into your interface. You're only specifying another contract.

灯下孤影 2024-09-19 17:31:02

是的。直接来自语言规范

内部类是未显式或隐式声明静态的嵌套类。

(粗体是我的):

嵌套类是其声明发生在另一个类或接口主体内的任何类。

Yes. Straight from the language spec:

An inner class is a nested class that is not explicitly or implicitly declared static.

And (boldface mine):

A nested class is any class whose declaration occurs within the body of another class or interface.

遗心遗梦遗幸福 2024-09-19 17:31:02

我发现非常有用的一个用例是,如果您有一个创建接口实例的构建器。如果构建器是接口的静态成员,则可以创建如下实例:

DigitalObject o = new DigitalObject.Builder(content).title(name).build();

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:

DigitalObject o = new DigitalObject.Builder(content).title(name).build();
甜心 2024-09-19 17:31:02

这是合法的,但我实际上只使用嵌套接口(如前所述)或嵌套枚举来做到这一点。例如:

public interface MyInterface {

    public enum Type { ONE, TWO, THREE }

    public Type getType();

    public enum Status { GOOD, BAD, UNKNOWN }

    public Status getStatus();

}

It is legal, but I only really do it with nested interfaces (as already mentioned) or nested enums. For example:

public interface MyInterface {

    public enum Type { ONE, TWO, THREE }

    public Type getType();

    public enum Status { GOOD, BAD, UNKNOWN }

    public Status getStatus();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文