Error

Class is Already defined

is not an enclosing class

確認內部類別的使用方式

public class DemoClass {

    private String name;

    public DemoClass(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void execute(){
        class LocalInnerClass {

            private String name;

            public LocalInnerClass(String name){
                this.name = name;
            }

            public String getName(){
                return name;
            }
        }
    }

    public class InnerClassA {

        private String name;

        public InnerClassA(String name){
            this.name = name;
        }

        public String getName(){
            return name;
        }
    }

    public static class InnerClassB {

        private String name;

        public InnerClassB(String name){
            this.name = name;
        }

        public String getName(){
            return name;
        }
    }

}

class InnerClassC {

    private String name;

    public InnerClassC(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }
}

main

public static void main(String[] args) {
    DemoClass demoClass = new DemoClass("123");
    DemoClass.InnerClassA innerClassA = demoClass.new InnerClassA("123");
    DemoClass.InnerClassB innerClassB = new DemoClass.InnerClassB("123");
    InnerClassC innerClass = new InnerClassC("123");
}

Last updated