2013-07-23 197 views
1

這裏是我的類的..如何使用非靜態函數內部靜態函數

public class Oop { 
    int count = 0; 

    public static void main(String args[]) 
    { 
     this.count(15, 30); 
     System.out.print(this.count); 
    } 

    public void count(int start, int end) 
    { 
     for(;start<end; start++) 
     { 
      this.count = this.count + start; 
     } 
    } 
} 

我不能叫主函數中的計數功能。原因是靜態和非靜態函數。我對Java真的很陌生。我如何使用主內的計數?我需要學習什麼?

+3

,*對錯誤信息搜索* [移除非通用名稱],因爲有很多重複。你*必須*創建一個實例來調用一個非靜態方法。 – user2246674

回答

2

你應該讓count功能和count變量static太。

1

這是您最擔心的問題 - 一旦您調用該方法,您將無法訪問結果。

您必須創建類的實例,並使用該實例通話你的方法得到的結果:現在

public static void main(String args[]) { 
    Oop oop = new Oop(); 
    oop.count(15, 30); 
    System.out.print(oop.count); 
}