2013-02-13 39 views
0

我有這樣的代碼:包含Swarm.SwarmEnemy.SwarmEnemy1一個封閉的情況下,需要

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); 
      } 
     } 

    } 
} 

我試圖讓工廠裝飾。無論如何 - 這是我得到的錯誤:包含Swarm.SwarmEnemy.SwarmEnemy1的封閉實例是必需的這是什麼意思,我應該如何重構我的代碼工作?

回答

1

你需要移動SwarmEnemy1SwarmEnemy2SwarmEnemy類。

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); 
     } 
    } 
} 
相關問題