课后作业 查询“你已经创建了多少个对象?”。
2015-10-17 16:50
134 查看
使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。
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+"个对象!");
}
}
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+"个对象!");
}
}
相关文章推荐
- Mac下用Brew安装(切换)的PHP版本
- Java 八大排序之——简单选择排序
- Z律师:创业项目如何玩转股权众筹?
- WF索引器7
- 架构师之路
- 架构师之路
- 数据结构考研复习--线性表3(约瑟夫环)
- SQLite数据库
- 属性操作
- WF数字计算6
- 如何在java中实现List集合的遍历
- POJ 2105解题报告
- SynchronizationContext一篇
- 秦时明月-白雪
- jquery排除某个元素
- poj 1743(后缀数组)
- Android学习Broadcast Receiver(广播接收者) 注意事项
- android去掉 radio 或 checkBox 的默认图标,用自己的图标
- get post 区别
- WF累加求和5