一个类,在任何时候都可以向它查询“你已经创建了多少个对象!
import java.util.Scanner;
public class Number {
private static int i = 0;
public Number()
{
i++;
}
public static int getNumber()
{
return i;
}
public static void main(String[] args) {
Number t1 = new Number();
Number t2 = new Number();
Number t3 = new Number();
Number t4 = new Number();
System.out.println("一共创建了" + Number.getNumber() + "个对象");
}
}