2013-07-23 139 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); 
}