Java代码输出很奇怪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24923929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:45:48  来源:igfitidea点击:

Java code output is weird?

java

提问by user3871104

I do not understand why my output is not what I expected, instead of showing the persons information, the output displays: examples.Examples@15db9742

我不明白为什么我的输出不是我所期望的,而不是显示人员信息,输出显示: examples.Examples@15db9742

Am I doing something wrong in my code?

我的代码做错了吗?

package examples;

public class Examples {
    String name;
    int age;
    char gender;

    public Examples(String name, int age, char gender){
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public static void main(String[] args) {
        Examples[] person = new Examples[10];
        person[0] = new Examples("Doe",25,'m');
        System.out.println(person[0]);
    }

}

采纳答案by takendarkk

When you say

当你说

System.out.println(person[0]);

java doesn't automatically know what you want printed out. To tell it, you write a method in your Examplesclass called toString()which will return a string containing the info you want. Something like:

java 不会自动知道你想打印什么。告诉它,你在你的Examples类中编写了一个方法,toString()它会返回一个包含你想要的信息的字符串。就像是:

@Override
public String toString() {
    return "Name: " + name + 
           " Age: " + String.valueOf(this.age) + 
           " Gender: " + String.valueOf(this.gender); 
}

回答by sergioFC

Your output is right, when you print an object the method toString() of the object is called; by default it returns what you see (the class and a memory direction). Override the method toString() of the class to make him return a descriptive String. E.g.:

您的输出是正确的,当您打印对象时,会调用该对象的 toString() 方法;默认情况下,它返回您看到的内容(类和内存方向)。覆盖类的方法 toString() 使他返回一个描述性的字符串。例如:

public class Examples {
   // The same ...
   public String toString(){
      return "My name is " + name + " and I have " + age + " years."
   }
   // The same ...
}

If you do that you will get a more descriptive String when calling toString() and so when printing an object of class Examples. New output is

如果这样做,您将在调用 toString() 时获得更具描述性的字符串,因此在打印示例类的对象时也是如此。新输出是

My name is Dow and I have 25 years.

回答by ulyssesrr

personis an array of type Examples, so by acessing person[0]you are telling it to print an Examplesinstance. Since the Examplesclass does not implement an toString()method it will call the parent Object.toString()method that produces the output you are seeing.

person是一个 type 数组Examples,所以通过访问person[0]你告诉它打印一个Examples实例。由于Examples该类没有实现toString()方法,它将调用Object.toString()产生您所看到的输出的父方法。

Add the following method to your Examples class

将以下方法添加到您的示例类

public String toString() {
    return "[name="+this.name+", age="+this.age+", gender="+this.gender+"]";
}

回答by Xinzz

Add a toString()method to your class:

toString()给你的类添加一个方法:

public class Examples {
String name;
int age;
char gender;

public Examples(String name, int age, char gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
}

@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    result.append(this.name + " ");
    result.append(this.age + " ");
    result.append(this.gender + " ");
    return result.toString();
}

public static void main(String[] args) {
   Examples[] person = new Examples[10];
   person[0] = new Examples("Doe",25,'m');
   System.out.println(person[0]);
}

}

回答by alomeli

Java has no way of knowing what you want it to print. By default, the toString() method is called when you use System.out.println() with an object.

Java 无法知道您希望它打印什么。默认情况下,当您将 System.out.println() 与对象一起使用时,会调用 toString() 方法。

Your Examples class should have its own toString() method so you can decide what to print. The default toString() returns a representation of the object in memory.

您的示例类应该有自己的 toString() 方法,以便您可以决定要打印的内容。默认 toString() 返回内存中对象的表示。

For example, to print out the object's name:

例如,要打印出对象的名称:

package examples;
public class Examples {

    ...

    @Override
    public String toString() {
        return name;
    }

}

回答by user14312287

You have explicitly to create a method which outputs the persons data or override the toString() method to do the same thing:

您必须明确创建一个输出人员数据的方法或覆盖 toString() 方法来做同样的事情:

public class Person
{
    String name;
    int age;
    char gender;

    public Person(String name, int age, char gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    //Override the toString() method 
    //is a usual programming technique 
    //to output the contents of an object
    public String toString()
    {
        return "Name: " + this.name + "\nAge: " + this.age + "\nGender: "
                + this.gender;
    }


    //You can also write something like this
    public void showInfo()
    {
        System.out.printf("Persons Info:\n\nName: %s\nAge: %s\nGender: %s", this.name, this.age, this.gender);
    }

    public static void main(String[] args)
    {
        Person p = new Person("bad_alloc", 97, 'm');

        //System.out.println("Persons info:\n" + p.toString());
        //If you want directly to "output the object" you have to override the toString() method anyway:

        //System.out.println(p);//"Outputting the object", this is possible because I have overridden the toString() method

        p.showInfo();
    }
}