为什么我的 toString() 打印这个?
Why does my toString() print this?
public static void main(String args[]){
Person p = new Person();
System.out.println(p.toString());
}
这是我在 class 上调用 toString 的地方。
class 是:
public class 人 {
private String FirstName;
private String LastName;
private int age;
private int salary;
public void setFN(String NewName)
{FirstName = NewName;}
public void setLN(String NewName)
{LastName = NewName;}
public void setAge(int NewAge)
{age = NewAge;}
public void setSalary(int NewSalary)
{salary = NewSalary;}
public String getFN()
{return FirstName;}
public String getLN()
{return LastName;}
public int getAge()
{return age;}
public int getSalary()
{return salary;}
}
它打印出这个:
Person@7852e922
我想知道它为什么打印这个以及它是什么。我的老师给我布置了这个作业,但我在 google 或任何其他地方都找不到任何内容。
根据http://www.javabeginner.com/learn-java/java-tostring-method
toString()
默认输出为:
Class 名称 + @ + 对象哈希码的十六进制版本 连接成一个字符串。
Object中默认的哈希码方式一般是通过将对象的内存地址转换为整数来实现的。
您看到的输出来自 Java 默认 toString
方法:
public String toString()
Returns a string representation of the
object. In general, the toString method returns a string that
"textually represents" this object. The result should be a concise but
informative representation that is easy for a person to read. It is
recommended that all subclasses override this method.
The toString
method for class Object returns a string consisting of the name of the
class of which the object is an instance, the at-sign character `@',
and the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns: a string representation of the object.
public static void main(String args[]){
Person p = new Person();
System.out.println(p.toString());
}
这是我在 class 上调用 toString 的地方。 class 是: public class 人 {
private String FirstName;
private String LastName;
private int age;
private int salary;
public void setFN(String NewName)
{FirstName = NewName;}
public void setLN(String NewName)
{LastName = NewName;}
public void setAge(int NewAge)
{age = NewAge;}
public void setSalary(int NewSalary)
{salary = NewSalary;}
public String getFN()
{return FirstName;}
public String getLN()
{return LastName;}
public int getAge()
{return age;}
public int getSalary()
{return salary;}
}
它打印出这个:
Person@7852e922
我想知道它为什么打印这个以及它是什么。我的老师给我布置了这个作业,但我在 google 或任何其他地方都找不到任何内容。
根据http://www.javabeginner.com/learn-java/java-tostring-method
toString()
默认输出为:
Class 名称 + @ + 对象哈希码的十六进制版本 连接成一个字符串。
Object中默认的哈希码方式一般是通过将对象的内存地址转换为整数来实现的。
您看到的输出来自 Java 默认 toString
方法:
public String toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns: a string representation of the object.