JAVA小练习46——统计一个类创建对象的个数

    xiaoxiao2022-07-03  187

    class Person{ String name; int id; public Person(int id,String name) { this.name=name; this.id=id; } static int count=0;//count被所有对象共享 { count++; } } public class Demo46 { public static void main(String[] args) { Person p1 = new Person(110,"狗娃1"); Person p2 = new Person(110,"狗娃2"); Person p3 = new Person(110,"狗娃3"); System.out.println("创建对象的个数:"+Person.count); } }
    最新回复(0)