eclipse - 输出
我有一个 Java 程序 - 它使用遗传算法 - 在其中生成一个随机二进制字符串。从这里,我计算所述字符串的适合度并将适合度打印到输出控制台。
我现在尝试显示该字符串,但我在执行此操作时遇到了问题。我的目的是显示该字符串,然后通过更改一个基因来对其进行变异,然后重新计算适应度。但是,每当我尝试打印字符串时,我都会在输出窗口中收到以下内容:
Individual@23fc4bec //FYI: Class name is 'Individual'
这是我的代码 - 在 main
方法中 - 到目前为止:
Individual newIndi = new Individual();
System.out.println(newIndi.fitness);
System.out.print(newIndi);
问题发生在最后一行 - 我也尝试过以下:
System.out.print(newIndi.toString());
但这仍然会产生类似的结果。
I have a Java program - which uses genetic algorithms - in which I generate a random binary string. From here, I calculate the fitness of said string and print the fitness to the output console.
I am now attempting to display the string, but I am having trouble doing this. My intention is to display the string, then mutate it by changing one gene, and then recalculate the fitness. However, whenever I try to print the string, I receive this in the output window:
Individual@23fc4bec //FYI: Class name is 'Individual'
This is my code - in the main
method - so far:
Individual newIndi = new Individual();
System.out.println(newIndi.fitness);
System.out.print(newIndi);
The problem occurs on the last line - I have also attempted the following:
System.out.print(newIndi.toString());
but this still produces a similar result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要重写
Individual
类中的 toString 方法。注意:eclipse 可以为您的类生成一些基本的 toString() 方法(Source >Generate toString())。
You need to override the toString method in you
Individual
-Class.Note: eclipse can generate you some basic toString() methode for your class (Source > Generate toString()).
如果您只是尝试在另一个类中打印私有变量,则重写您的Individual 类中的toString 方法将允许您返回该变量(正如其他人所说)。只需创建一个 toString 方法:
默认情况下,尝试仅打印对象名称将调用 toString 方法,如果不重写该方法,该方法将返回该对象的内存位置。
另一个想法可能只是创建一种新方法。
并做
If you're simply trying to print a private variable in another class, overriding the toString method in your Individual class would allow you to return that variable (as others have said). Simply create a toString method:
Trying to just print the object name will, by default, call the toString method, which will return the memory location of the object if you do not override it.
Another idea might simply be to create a new method.
and do
toString
方法由java.lang.Object
实现,返回对象的类名及其 hashCode。如果您想打印除此之外的内容,则需要重写 toString 并使其返回您想要的内容。请注意,
System.out.print(newIndi);
调用newIndi
上的toString
方法。您不需要显式调用toString()
。The
toString
method is implemented byjava.lang.Object
, and returns the class name of the object followed by its hashCode. If you want to print something other than that, you need to override toString and make it return what you desire.Note that
System.out.print(newIndi);
calls thetoString
method onnewIndi
. You don't need to explicitely calltoString()
.您需要重写Individual中的toString()方法才能实际输出类的内容。默认的 Object toString() 将返回上面的内容。
You need to override the toString() method in Individual to actually output the content of the class. The default Object toString() will return what you have above.
如果您想这样做,您的单个类应该重新实现 toString 方法,否则它将打印 Java 参考(如您所见)。 System.out.println(newIndi) 调用 toString 方法。
your Individual Class should reimplement the toString method, if you want to do it this way, or else the it will print the Java Reference (as you see). System.out.println(newIndi) calls the toString method.