eclipse - 输出

发布于 2024-12-08 06:24:53 字数 561 浏览 12 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

数理化全能战士 2024-12-15 06:24:53

您需要重写 Individual 类中的 toString 方法。

public class Individual {
    // ...

    @Override
    public String toString() {
      // output
    }
}

注意:eclipse 可以为您的类生成一些基本的 toString() 方法(Source >Generate toString())。

You need to override the toString method in you Individual-Class.

public class Individual {
    // ...

    @Override
    public String toString() {
      // output
    }
}

Note: eclipse can generate you some basic toString() methode for your class (Source > Generate toString()).

地狱即天堂 2024-12-15 06:24:53

如果您只是尝试在另一个类中打印私有变量,则重写您的Individual 类中的toString 方法将允许您返回该变量(正如其他人所说)。只需创建一个 toString 方法:

public String toString()
{
    return whateverYourVarNameIs;
}

默认情况下,尝试仅打印对象名称将调用 toString 方法,如果不重写该方法,该方法将返回该对象的内存位置。

另一个想法可能只是创建一种新方法。

public String getGenes()
{
    return whateverYourVarNameIs;
}

并做

System.out.println(whateverYourVarNameIs);

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:

public String toString()
{
    return whateverYourVarNameIs;
}

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.

public String getGenes()
{
    return whateverYourVarNameIs;
}

and do

System.out.println(whateverYourVarNameIs);
染柒℉ 2024-12-15 06:24:53

toString 方法由 java.lang.Object 实现,返回对象的类名及其 hashCode。如果您想打印除此之外的内容,则需要重写 toString 并使其返回您想要的内容。

请注意,System.out.print(newIndi); 调用 newIndi 上的 toString 方法。您不需要显式调用 toString()

The toString method is implemented by java.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 the toString method on newIndi. You don't need to explicitely call toString().

不回头走下去 2024-12-15 06:24:53

您需要重写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.

陌上青苔 2024-12-15 06:24:53

如果您想这样做,您的单个类应该重新实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文