方法内的局部内部类的数量是否有限制?

发布于 2024-11-04 09:10:58 字数 476 浏览 4 评论 0原文

我在处理一些 java 代码时遇到了一些困难

class foo{
   public bar() {
      class innerA {}
      class innerB {} // Only this one is valid because it was declare last
   }
}

我的问题:只有最后声明的内部类 (innerB) 在 foo::bar() 中可见。此外,我无法从另一个内部类中引用另一个内部类。示例:

innerB{
    private innerA _a; // Error
}

我的问题:方法中可以拥有的本地内部类的数量是否有限制?本地内部类可以实例化其他本地内部类对象吗?他们应该吗?

编辑:我在 IDE 中输入错误,并且遇到了一些范围问题...再次感谢!

TIA, 小白

I'm having a bit of difficulty with some java code

class foo{
   public bar() {
      class innerA {}
      class innerB {} // Only this one is valid because it was declare last
   }
}

My Problem: Only the last declared inner class (innerB) is visible within foo::bar(). Additionally, I cannot reference either inner class from within the other. Example:

innerB{
    private innerA _a; // Error
}

My Question: Is there some limit on the number of local inner classes you can have within a method? Can local inner classes instantiate other local inner class object? Should they?

EDIT: I miss-typed in my IDE and had some scoping issues... thanks again!

TIA,
noob

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

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

发布评论

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

评论(2

红ご颜醉 2024-11-11 09:10:58

请发布您真正尝试运行的代码,以及您从 Java 编译器获得的错误消息。您发布的代码不是有效的 Java。你想做的一切都是可行的。这是一个工作示例:

public class Foo {
    public void bar() {
        class InnerA {
            public void hello() {
                System.out.println("Hello from InnerA");
            }
            public String getName() {
                return "InnerA";
            }
        }

        class InnerB {
            private InnerA aInB = new InnerA();
            public void hello() {
                System.out.println("Hello from InnerB");
                System.out.println("In InnerB, got name of InnerA : " + aInB.getName());
            }
        }

        InnerA a = new InnerA();
        InnerB b = new InnerB();

        a.hello();
        b.hello();
    }

    public static void main(String[] args) {
        new Foo().bar();
    }
}

运行它给出:

Hello from InnerA
Hello from InnerB
In InnerB, got name of InnerA : InnerA

Please post the code you really try to make work, and the error message you get from the Java compiler. The code you posted isn't valid Java. Everything you want to do is doable. Here's a working example :

public class Foo {
    public void bar() {
        class InnerA {
            public void hello() {
                System.out.println("Hello from InnerA");
            }
            public String getName() {
                return "InnerA";
            }
        }

        class InnerB {
            private InnerA aInB = new InnerA();
            public void hello() {
                System.out.println("Hello from InnerB");
                System.out.println("In InnerB, got name of InnerA : " + aInB.getName());
            }
        }

        InnerA a = new InnerA();
        InnerB b = new InnerB();

        a.hello();
        b.hello();
    }

    public static void main(String[] args) {
        new Foo().bar();
    }
}

Running it gives :

Hello from InnerA
Hello from InnerB
In InnerB, got name of InnerA : InnerA
指尖微凉心微凉 2024-11-11 09:10:58

您忽略了为 bar() 指定返回类型。

You neglected to specify a return type for bar().

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