java一个能记录生成多少个对象的类

//请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。
package Text1;

public class Find
{
static int x=0;//记录创建对象个数的变量
int a;
public Find()//重写构造函数
{
a=0;
x++;//生成一个对象,x++
}
public void Output()//输出函数
{
System.out.println(a);
}
public static void main(String[] args)
{
Find f=new Find();//创建对象
Find e=new Find();
Find r=new Find();
Find g=new Find();
Find h=new Find();
System.out.println(x);//输出创建对象个数
}
}

输出:5

posted @ 2017-10-18 14:56  夏延  阅读(469)  评论(0编辑  收藏  举报