Discuss / Java / 练习

练习

Topic source
public class Main {

	public static void main(String[] args) {
		// TODO: 给Person类增加一个静态字段count和静态方法getCount,统计实例的个数
		Person p1 = new Person("小明");
		Person p2 = new Person("小红");
		Person p3 = new Person("小军");
		Person p4 = new Person("小牛");
		System.out.println(Person.getCount()); // 4
	}

}
class Person {

    protected static int count;
	String name;

	public Person(String name) {
		this.name = name;
		count++;
	}
	public static int getCount() {
		return count;
	}
}
protected static int count;protected static int count;

  • 1

Reply