chap4-15.txt

来自「清华大学出版社经典教材系列」· 文本 代码 · 共 30 行

TXT
30
字号
// 程序4-15
class point{  
    static int count;		// 定义静态变量
    int x, y;
    
    static{
        count=0;
        System.out.println("static variable is initialized !");		
    }
  
    point(int a, int b){
        count++;		// 对象计数器
        x=a;  y=b;		
        System.out.println("Call point constructor!");
    }
	
    static  int  getCount( ){     	// 静态方法
        return  count;  		// 写成this.count 是错误的
    }
}

public class testStaticMethod {
    public static void main(String args[ ]) {
        point c1=new point(0,0);
        point c2=new point(1,1);
        
        System.out.println("There are "+ point.getCount( ) +" points");
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?