标签:main return void 调用 ati http auto img 字段
题目就要求:
使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数
设计思路:
创建一个类并创建一个构造函数,因为程序每次创建对象时都会自动调用构造函数所以在每次构造函数时在函数体内的time加1即可显示一共创建了多少个对象。
程序源码:
package homework3;
class Number
{
static int times;
public Number()
{
times++;
}
int GetTimes()
{
return times;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Number n1=new Number();
Number n2=new Number();
Number n3=new Number();
System.out.println("你已经创建了"+Number.times+"对象");
}
}
截图:
标签:main return void 调用 ati http auto img 字段
原文地址:http://www.cnblogs.com/sin30/p/7698509.html