Java代码输出很奇怪?

时间:2014-07-24 01:58:47

标签: java

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

我在代码中做错了吗?

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]);
    }

}

6 个答案:

答案 0 :(得分:2)

向您的班级添加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]);
}

}

答案 1 :(得分:1)

当你说

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

java不会自动知道您要打印的内容。要告诉它,您在名为Examples的{​​{1}}类中编写了一个方法,该方法将返回包含所需信息的字符串。类似的东西:

toString()

答案 2 :(得分:1)

Java无法知道您想要打印的内容。默认情况下,将System.out.println()与对象一起使用时,将调用toString()方法。

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

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

package examples;
public class Examples {

    ...

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

}

答案 3 :(得分:0)

您的输出是正确的,当您打印对象时,会调用对象的方法toString();默认情况下,它返回您看到的内容(类和内存方向)。 重写类的方法toString()以使其返回描述性字符串。 E.g:

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

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

My name is Dow and I have 25 years.

答案 4 :(得分:0)

person是一个Examples类型的数组,因此通过访问person[0],您告诉它打印Examples实例。由于Examples类未实现toString()方法,因此它将调用生成您正在看到的输出的父Object.toString()方法。

将以下方法添加到Examples类

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

答案 5 :(得分:0)

您已明确创建一个输出人员数据的方法或覆盖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();
    }
}