03-类与对象

使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。

 1 public class ppt3 {
 2     static int num=0;
 3     ppt3(){
 4         num=num+1;
 5     }
 6     public static void main(String[]srgs){
 7         ppt3 a=new ppt3();
 8         ppt3 b=new ppt3();
 9         ppt3 c=new ppt3();
10         System.out.println("类的成员的个数为"+ppt3.num);
11     }
12 
13 }

 

posted @ 2017-10-20 15:32  sTrive。  阅读(124)  评论(0编辑  收藏  举报