繁体   English   中英

Java嵌套嵌套类

[英]Java nested nested class

我很好奇(如果?)嵌套的嵌套类应该如何工作。

Eclipse Oxygen.3 没有提到“对象团队”,但是我认为以下代码举例说明了一些反模式,因为我没有看到嵌套嵌套类的业务需求。

public class CACCIData {
...
    public CompanySearchResultsResult.CompanySearchResult mcCompanySearchResult = null;

    public static class CompanySearchResultsResult {
        public List<CompanySearchResult> CompanySearchResult 
            = new ArrayList<CompanySearchResult>();

        public static class CompanySearchResult {
// ...
        }
    }

Eclipse 2018-12 编译错误: 'CACCIData$CompanySearchResultsResult' cannot be used as type anchor for an externalized role: is not a team (OTJLD 1.2.2(b)).

双嵌套类在语法上是有效的。 下面的代码工作并打印逻辑的、可预测的输出:

public class Test {
    public static void main(String[] args) {
        new A().testPrintA();
    }
    public static class A {
        public void testPrintA() {
            System.out.println("Works from A.");
            new B().testPrintB();
        }
        public static class B {
            public void testPrintB() {System.out.println("Works from B.");}
        }
    }
}

现在在“你应该使用它”方面......这是自以为是,但我相信这种构造提供最佳解决方案的机会不会太多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM