0

I have such code:

public class Swarm {

    public SwarmEnemy getEnemy() {
        return new SwarmEnemy.SwarmEnemy1(SwarmEnemy.NORMAL_SWARM); // <-ERROR
    }

    private class SwarmEnemy extends BeamEnemy {

        public static final int NORMAL_SWARM = 0;

        public SwarmEnemy(int hp, int swarmType) {
            super(0, 0, hp, 0);
            switch (swarmType) {
                case 0:
                    System.out.println("constructor 1");
                    break;
            }
        }

        private class SwarmEnemy1 extends SwarmEnemy {

            public SwarmEnemy1(int swarmType) {
                super(25, swarmType);
            }
        }

        private class SwarmEnemy2 extends SwarmEnemy {

            public SwarmEnemy2(int swarmType) {
                super(25, swarmType);
            }
        }

    }
}

I tried to make Factory decorator. Anyway - this is error I get: An enclosing instance that contains Swarm.SwarmEnemy.SwarmEnemy1 is required What does it mean and how should I refactor my code to work?

0

1 Answer 1

1

You need to move SwarmEnemy1 and SwarmEnemy2 outside SwarmEnemy Class.

public class Swarm {

    public SwarmEnemy getEnemy() {
        return new SwarmEnemy1(SwarmEnemy.NORMAL_SWARM); // <-ERROR
    }

    private class SwarmEnemy extends BeamEnemy {

        public static final int NORMAL_SWARM = 0;

        public SwarmEnemy(int hp, int swarmType) {
            super(0, 0, hp, 0);
            switch (swarmType) {
                case 0:
                    System.out.println("constructor 1");
                    break;
            }
        }
    }

    private class SwarmEnemy1 extends SwarmEnemy {

        public SwarmEnemy1(int swarmType) {
            super(25, swarmType);
        }
    }

    private class SwarmEnemy2 extends SwarmEnemy {

        public SwarmEnemy2(int swarmType) {
            super(25, swarmType);
        }
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.