1 package 练习; 2 3 public class lianxi { 4 private static int a; 5 public lianxi() 6 { 7 a++; 8 } 9 public static int get() 10 { 11 return a; 12 } 13 public static void main(String[] args) { 14 15 lianxi x1 =new lianxi(); 16 lianxi x2=new lianxi(); 17 System.out.println("共创建对象的个数为"+lianxi.get()+"个"); 18 } 19 }
编写构造函数,以及get函数,定义两个对象,最后调用get函数来计算一共定义个多少个对象