testperson.java

来自「东软的java初级教程」· Java 代码 · 共 28 行

JAVA
28
字号
class Person {
       private int id;
       private static int total = 0;
       public static int getTotalPerson() { 
	//id++; //illeagle
	return total;
       }
       /*public static void setTotalPerson(int total){
       		this.total=total;//在static方法中不能有this
       	}
       	*/
       public Person() {
         	total++;
 	id = total;
       }
}

public class TestPerson {
        public static void main(String[] args) {
 	System.out.println("Number of total is "
		 +Person.getTotalPerson());//没有创建对象也可以访问静态方法
 	Person p1 = new Person();
     	System.out.println( "Number of total is "
 		+ Person.getTotalPerson());
 	//Person.setTotalPerson();
        }
}

⌨️ 快捷键说明

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