统计建立对象的个数

 1 package cs;
 2 public class cs{
 3     public static int amount=0;
 4     cs(){
 5         amount++;
 6     }
 7     public int count() {
 8         return amount;
 9     }
10     public static void main(String[] args) {
11         cs[] a=new cs[4];
12         for(int i=0;i<a.length;i++) {
13             a[i]=new cs();
14             System.out.println("这是建立的第"+a[i].count()+"个对象!");
15         }
16     }
17 }

用全局变量进行统计。

原文地址:https://www.cnblogs.com/quxiangjia/p/9825617.html