public class Text
{
public static int num;//静态字段
public Text()
{
num++;
System.out.printf("你已经创建了%d个对象\n",num);
}//构造函数
public static void main(String[] args)
{
num=0;
Text a=new Text();
Text b=new Text();
Text c=new Text();
}
}
这个函数每次调用都会对num进行加1并且输出这是第几次运行程序,从而完成对函数的跟踪。