给Person类增加一个静态字段count和静态方法getCount,统计实例创建的个数。
public class Person {
public String name;
public int age;
public static int count=0;
public Person(String name,int age){
this.name =name;
this.age =age;
count++;
}
public static int getCount(){
return count;
Sign in to make a reply
阳光透过窗台201702
给Person类增加一个静态字段count和静态方法getCount,统计实例创建的个数。
public class Person {
public String name;
public int age;
public static int count=0;
public Person(String name,int age){
this.name =name;
this.age =age;
count++;
}
public static int getCount(){
return count;
}
}