package java5;
public class Cobject {
static int num=0;
public Cobject()
{
num++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Cobject C1=new Cobject();
Cobject C2=new Cobject();
Cobject C3=new Cobject();
System.out.println("目前你已创建了"+Cobject.num+"Cobject个对象");
}
}
使用类的静态字段和构造函数,可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”
原文:https://www.cnblogs.com/zaixiachengxuyuan/p/11696443.html