使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”
package t2;
class JiShu
{
static int i=0;
public JiShu()
{
i++;
System.out.println("共建立了"+i+"个类");
}
}
public class t2
{
public static void main(String[] args)
{
JiShu a1=new JiShu();
JiShu a2=new JiShu();
JiShu a31=new JiShu();
JiShu a4=new JiShu();
JiShu a5=new JiShu();
//a5.JiShu();
}
}