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

 

public class Student{

public static void main(String[] args){

Information a = new Information();

a.OutputInformation();

Information b = new Information();

b.OutputInformation();

} }

class Information{

static int num=0;

Information()

{ num++;

}

public void OutputInformation()

 {

System.out.println("你已经创建了"+num+"个对象!");

}

}

课后作业   查询“你已经创建了多少个对象?”。

相关文章:

  • 2021-08-29
  • 2021-09-11
  • 2022-02-14
  • 2021-09-25
  • 2021-08-25
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-11-07
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案