2013-09-22 115 views
3

我正在創建一個Slick2D遊戲。現在,我創建一個Video類,其中包含內部類(FrameSize,FPS,FullScreen ..)。所以我有一個OOD的想法,就像我們稱之爲System.out.println()。這意味着我將擁有公開的Video類和公共靜態實例,但netbeans IDE給我提示了「通過公共API導出非公共類型」的提示。那麼,我應該忽略這一點,並繼續按照自己的方式行事,否則如果您可以將您的想法提供給我,這會很棒嗎?Netbeans警告:通過公共API導出非公共類型

VIDEO

public class Video { 

    public static FrameSize frameSize; 
    public static FullScreen fullScreen; 
    public static FPS fps; 

    private Video() {} 

    public static void loadArguments(Scanner loadInput) { 
     boolean isVideo = false; 
     String readLine; 

     while (loadInput.hasNext()) { 
      readLine = loadInput.next(); 
      if (readLine.equalsIgnoreCase("video")) { 
       isVideo = true; 
       break; 
      } 
     } 

     while (isVideo && loadInput.hasNext()) { 
      readLine = loadInput.next(); 
      if (readLine.equalsIgnoreCase("end")) { 
       break; 
      } 
      String[] line = readLine.split("="); 

      String key = line[0]; 
      String value = line[1]; 

      switch (key) { 
       case "width": 
        frameSize.setWidth(Integer.parseInt(value)); 
        break; 
       case "height": 
        frameSize.setHeight(Integer.parseInt(value)); 
        break; 
       case "fullscreen": 
        break; 
       case "fps": 
        break; 
       default: 
        System.err.println("Unknown video key: " + key); 
        break; 
      } 
     } 
    } 

    public static void saveArguments(String filePath) { 
     Scanner saveInput; 
     try { 
      saveInput = new Scanner(new File(filePath)); 
     } catch (FileNotFoundException fne) { 
      System.err.println("Invalid settings-file."); 
      return; 
     } 

     // TO DO: save function 

     saveInput.close(); 
    } 

    class FrameSize { 

     public final int[][] SIZE_VALUES = { 
       {800, 600}, 
       {1000, 700}, 
       {1200, 800}, 
       {1400, 900} 
     }; 

     private int index; 
     private int width, height; 

     private FrameSize() {} 

     public void setSize(int width, int height) { 
      this.width = width; 
     } 

     public int getWidth() { 
      return width; 
     } 

     public void setWidth(int width) { 
      this.width = width; 
     } 

     public int getHeight() { 
      return height; 
     } 

     public void setHeight(int height) { 
      this.height = height; 
     } 

     @Override 
     public String toString() { 
      return this.width + " x " + this.height; 
     } 

    } 

    class FullScreen { 

     private boolean fullScreen; 

     private FullScreen() {} 

     public boolean isFullScreen() { 
      return fullScreen; 
     } 

     public void setFullScreen(boolean fullScreen) { 
      this.fullScreen = fullScreen; 
     } 

     @Override 
     public String toString() { 
      return "" + fullScreen; 
     }   
    } 

    class FPS { 

     private boolean FPS; 

     private FPS() {} 

     public boolean isFPS() { 
      return FPS; 
     } 

     public void setFPS(boolean FPS) { 
      this.FPS = FPS; 
     }   

     @Override 
     public String toString() { 
      return "" + fps; 
     } 

    } 

} 

AUDIO

public class Audio { 

    private static Sound sound; 
    private static Volume volume; 

    private Audio() {} 

    public void loadArguments(Scanner loadInput) { 
     boolean isAudio = false; 
     String readLine; 

     while (loadInput.hasNext()) { 
      readLine = loadInput.next(); 
      if (readLine.equalsIgnoreCase("audio")) { 
       isAudio = true; 
       break; 
      } 
     } 

     while (isAudio && loadInput.hasNext()) { 
      readLine = loadInput.next(); 
      if (readLine.equalsIgnoreCase("end")) { 
       break; 
      } 
      String[] line = readLine.split("="); 

      String key = line[0]; 
      String value = line[1]; 

      switch (key) { 
       case "sound": 
        break; 
       case "volume": 
        break; 
       default: 
        System.err.println("Unknown audio key: " + key); 
        break; 
      } 
     } 
    } 

    public void saveArguments(String filePath) { 
     Scanner saveInput; 
     try { 
      saveInput = new Scanner(new File(filePath)); 
     } catch (FileNotFoundException fne) { 
      System.err.println("Invalid settings-file."); 
      return; 
     } 

     // TO DO: save function 

     saveInput.close(); 
    } 

    class Sound { 

     private boolean sound; 

     private Sound() {} 

     public boolean isSound() { 
      return sound; 
     } 

     public void setSound(boolean sound) { 
      this.sound = sound; 
     } 

     @Override 
     public String toString() { 
      return "" + sound; 
     }   
    } 

    class Volume { 

     private static final double PITCH = 0.1d; 
     private double volume; 

     private Volume() {} 

     public double getVolume() { 
      return volume; 
     } 

     public void setVolume(double volume) { 
      this.volume = volume; 
     } 

     public void increaseVolume() { 
      if (!isVolumeRange(this.volume)) { 
       return; 
      } 
      this.volume = this.volume + PITCH; 
     } 

     public void decreaseVolume() { 
      if (!isVolumeRange(this.volume)) { 
       return; 
      } 
      this.volume = this.volume - PITCH; 
     } 

     public boolean isVolumeRange(double volume) { 
      return volume >= 0.0 && volume <= 10.0; 
     } 

    } 

} 

回答

8

Video類包含一個公共類變量FrameSize類型的frameSize的聲明。
A public修飾符的意思是,frameSize變量對所有人都是可見的。

package package1; 

public class Video { 
    public static FrameSize frameSize; 
} 
// private class 
class FrameSize { 
} 

然而FrameSize是本地類 - 它是可見只對同一個包的成員。另外,在上述例子中,只有包package1的成員可以看到類,和下面的代碼編譯細:

package package1; 

public class Test { 

    void test(){ 
     FrameSize x = Video.frameSize; 
    } 
} 

然而這個代碼(不同封裝)給出了一個編譯錯誤:

package package2; 
import package1.*; 

public class Test { 
    void test(){ 
    // this line won't compile - FrameSize class is unknown 
    FrameSize x = Video.frameSize; 

    // but this line compiles fine - Object class is public 
    Object y = Video.frameSize; 
    } 
} 

NetBeans的警告您關於這一點,因爲這很可能是無意的錯誤 - 爲什麼你想在不發佈這個字段的類型的情況下讓所有的字段值都可以訪問,這實際上阻止了他們使用該字段?

如果你想使變量只能訪問其他類中相同的包,聲明它爲protected,不public
但是如果這是一個有意的聲明 - 那麼忽略該警告並保持原樣。

+0

我明白了。我不知道那件事。然而,你能否告訴我另一個想法呢?這是我的第二個問題,因爲我們不推薦這樣做,因爲我肯定會在其他包中使用這個類,我應該讓它們成爲內部類作爲靜態類嗎? – nellykvist

+0

哦,等一下,這個警告是否適用於內部類,因爲您已經在視頻類之外的私人類的示例中向我展示了以上內容? – nellykvist

+0

那麼,我剛剛測試FramSize作爲視頻類的嵌套類,並將其設置爲公共,這是這樣做的正確方法? – nellykvist