1

我有一个与变量“totalFloat”有关的私人访问错误。但是,我一直在引用和使用同一个类的方法,没有这个问题。

在这种情况下,有没有一种通用的方法来解决私人访问错误?

public static void writeHtmlFile() {
  double contentsChangeDraw;

  String ChangeDrawer = "ChangeFloat.html";
  try {
    PrintWriter outputStream = new PrintWriter(ChangeDrawer); 
    contentsChangeDraw=cd.getTotalFloat(totalFloat); 

    // totalFloat has private access in ChangeDrawer, which means I am
    // unable to use the method to calculate the array that needs to be written  

    outputStream.println(contentsChangeDraw); 
    outputStream.close(); 
    System.out.println("The contents of the ChangeDrawer have been written to a file");
  } catch (FileNotFoundException e) {
    e.printStackTrace();      
  }       
}
4

1 回答 1

0

我敢打赌totalFloat有这样的声明:

private int totalFloat; // or double, or boolean, or something else

您不能从静态方法访问它,因为它是一个成员变量。做了

private static int totalFloat; // or double, or boolean, or something else

访问它(或将您的方法更改writeHtmlFile()为非静态)。

于 2012-07-26T08:29:30.593 回答