无法编译内部类源文件,导致错误?

发布于 2024-11-03 12:31:48 字数 1465 浏览 3 评论 0原文

似乎无法编译以下 main 方法:

public class MainMethod {

    public static void main(String []args){

        InnerizationClass outer = new InnerizationClass(); 
        InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1
        System.out.println(inner.getOuterClassVar());
        System.out.println(new InnerizationClass().locInnrMeth());
    }
}

这是 InnerizationClass 类:

public class InnerizationClass {

    String outerClassVar = "Outer Catism";
    static String outerClassVarStat = "Outer Static Catism";

    public static class StaticInnerClass{

        String innerClassVar = "Catism";

        public String getInnerClassVar(){

            return this.innerClassVar;
        }

        public String getOuterClassVar(){

            return InnerizationClass.outerClassVarStat;
        }
    }

    public String locInnrMeth(){

        class MethodLocalInnerClass{

            String methodLocalInrCls = "Method Local Catism";

            void printInner(){

                System.out.println(InnerizationClass.this.outerClassVar);
            }

        }

        return new MethodLocalInnerClass().methodLocalInrCls;   

    }

    public String getStaticOuterVar(){return null;}
}

我从 main 方法的#1 中收到以下错误:

MainMethod.java:6 qualified new of static class
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass();
1 error;

The seem not to be able to compile the following main method:

public class MainMethod {

    public static void main(String []args){

        InnerizationClass outer = new InnerizationClass(); 
        InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1
        System.out.println(inner.getOuterClassVar());
        System.out.println(new InnerizationClass().locInnrMeth());
    }
}

And this is the InnerizationClass class:

public class InnerizationClass {

    String outerClassVar = "Outer Catism";
    static String outerClassVarStat = "Outer Static Catism";

    public static class StaticInnerClass{

        String innerClassVar = "Catism";

        public String getInnerClassVar(){

            return this.innerClassVar;
        }

        public String getOuterClassVar(){

            return InnerizationClass.outerClassVarStat;
        }
    }

    public String locInnrMeth(){

        class MethodLocalInnerClass{

            String methodLocalInrCls = "Method Local Catism";

            void printInner(){

                System.out.println(InnerizationClass.this.outerClassVar);
            }

        }

        return new MethodLocalInnerClass().methodLocalInrCls;   

    }

    public String getStaticOuterVar(){return null;}
}

I am getting the following error from #1 from the main method:

MainMethod.java:6 qualified new of static class
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass();
1 error;

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

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

发布评论

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

评论(1

陪你搞怪i 2024-11-10 12:31:48

试试这个:

public class MainMethod {

public static void main(String []args){

    InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1
    System.out.println(inner.getOuterClassVar());
    System.out.println(new InnerizationClass().locInnrMeth());
 }
}

因为你的内部类是静态的,所以你不需要创建 InnerizationClass 的实例

Try this instead:

public class MainMethod {

public static void main(String []args){

    InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1
    System.out.println(inner.getOuterClassVar());
    System.out.println(new InnerizationClass().locInnrMeth());
 }
}

since your inner class is static you don't need to create an instance of InnerizationClass

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