03-类和对象——课后作业:跟踪某个类所创建对象的个数
程序源代码:
1 import java.util.Scanner; 2 3 public class NumberCount { 4 5 static int count=0; 6 7 NumberCount() 8 9 { 10 11 count += 1; 12 13 } 14 15 int getCount() 16 17 { 18 19 return count; 20 21 } 22 23 public static void main(String[] args) { 24 // TODO Auto-generated method stub 25 26 NumberCount num1 = new NumberCount(); 27 28 NumberCount num2 = new NumberCount(); 29 30 NumberCount num3 = new NumberCount(); 31 32 NumberCount num4 = new NumberCount(); 33 34 System.out.println("你已经创建了"+NumberCount.count+"个对象"); 35 36 } 37 38 }
执行结果: