英文:
How do I find the number of objects being created in a Java program?
问题
以下是翻译的内容:
代码应该统计创建的对象数量。
我预期它会创建三个对象,并且打印出一个 count
为 3
。
但实际上只打印出 1。我认为这是因为所有三个方法中的对象名称 Number_Objects obj
都是相同的。我该如何修改程序以便返回 3
(创建三个对象)?
public class Number_Objects {
static int count = 0;
Number_Objects() {
count++;
}
public void Number_Objectstest1() {
Number_Objects obj = new Number_Objects();
}
public void Number_Objectstest2() {
Number_Objects obj = new Number_Objects();
}
public void Number_Objectstest3() {
Number_Objects obj = new Number_Objects();
}
public static void main(String[] args) {
System.out.println(count);
}
}
英文:
The code presented should count the objects created.
I expected it to create three objects and a count
of 3
to be printed.
This only prints out 1. I think because the object name Number_Objects obj
is the same in all three of the methods. How do I change the program so that it returns 3
(creation of three objects)?
public class Number_Objects {
static int count = 0;
Number_Objects() {
count++;
}
public void Number_Objectstest1() {
Number_Objects obj = new Number_Objects();
}
public void Number_Objectstest2() {
Number_Objects obj = new Number_Objects();
}
public void Number_Objectstest3() {
Number_Objects obj = new Number_Objects();
}
public static void main(String[] args) {
System.out.println(count);
}
}
答案1
得分: 1
你提供的代码基本上只是打印文本,它不会输出 1
,而是输出 0
。
这是因为你在类的构造函数中更新了 count
变量(顺便提一句,这个类的命名不太好,请在Java中审查命名方案),然而构造函数从未被调用,因为你没有创建任何实例。
要得到结果为 3,你需要创建三个类的实例:
public static void main(String[] args) {
Number_Objects one = new Number_Objects();
System.out.println(Number_Objects.count); // --> 1
Number_Objects two = new Number_Objects();
System.out.println(Number_Objects.count); // --> 2
Number_Objects three = new Number_Objects();
System.out.println(Number_Objects.count); // --> 3
}
每次 new
都会为创建的类实例分配内存,并调用匹配你给定参数的适当构造函数。
英文:
The code you presented does basically nothing but printing text. Especially, it will not output 1
, it outputs 0
.
This is because you are updating the count
variable in the constructor of your class (which is badly named, btw. Please review naming schemes in Java), yet, the constructor is never called, because you create no instances.
To receive the result three, you need to create three instances of the class:
public static void main(String[] args) {
Number_Objects one = new Number_Objects();
System.out.println( Number_Objects.count ); // --> 1
Number_Objects two = new Number_Objects();
System.out.println( Number_Objects.count ); // --> 2
Number_Objects three = new Number_Objects();
System.out.println( Number_Objects.count ); // --> 3
}
Each new
will allocate memory for the instance of the class created and call the appropriate constructor, matching the parameters you give.
答案2
得分: 0
class NumberObjects {
static int count = 0;
NumberObjects() {
count++;
}
public static void NumberObjectstest1() {
NumberObjects obj = new NumberObjects();
}
public static void NumberObjectstest2() {
NumberObjects obj = new NumberObjects();
}
public static void NumberObjectstest3() {
NumberObjects obj = new NumberObjects();
}
public static void main(String[] args) {
NumberObjectstest1();
NumberObjectstest2();
NumberObjectstest3();
System.out.println(count);
}
}
英文:
You are creating an object inside the method. But you are not executing that function which means the object has not been created yet. Call your methods in the main method.
P.S. Java != JavaScript
class NumberObjects {
static int count = 0;
NumberObjects() {
count++;
}
public static void NumberObjectstest1() {
NumberObjects obj = new NumberObjects();
}
public static void NumberObjectstest2() {
NumberObjects obj = new NumberObjects();
}
public static void NumberObjectstest3() {
NumberObjects obj = new NumberObjects();
}
public static void main(String[] args) {
NumberObjectstest1();
NumberObjectstest2();
NumberObjectstest3();
System.out.println(count);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论