2012-07-26 86 views
1

我有一個與變量「totalFloat」有關的私人訪問錯誤。不過,我一直在引用和使用同一個類的方法,而沒有這個問題。私人訪問? Java

在這種情況下是否有避開私人訪問錯誤的一般方法?

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();  
    }  
} 
+2

totalFloat非靜態 – 2012-07-26 08:14:17

+4

totalFloat是如何聲明的? – 2012-07-26 08:14:29

+1

cd是什麼?它是ChangeDrawer類型的對象嗎? – Leon 2012-07-26 08:16:00

回答

0

我敢打賭totalFloat有這樣的聲明:

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

,因爲它是一個成員變量不能從靜態方法訪問它。讓它

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

訪問它(或改變你的方法writeHtmlFile()到不是靜態)。