码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-22 23:26:16      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:创建   main   count()   string   stat   method   void   get   turn   

package 课后作业3;
public class count
{
     public static int num;
  public count()
     {
         num ++; //统计个数,每创建一个类 num++
     }
     public static int getNum()
     {
         return num;  //返回得到num的最终的值
     }
 public static void main(String[] args)
 {
  // TODO Auto-generated method stub
  count c1 = new count();
  count c2 = new count();
  count c3 = new count();
  count c4 = new count();
  count c5 = new count();
  count c6 = new count();
        System.out.println("创建对象的个数是:"+count.getNum());
 }
}

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

标签:创建   main   count()   string   stat   method   void   get   turn   

原文地址:http://www.cnblogs.com/ljl1998/p/7712231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!