The object class is the parent class of all classes, arrays, and enumeration classes, and is the root class of the class hierarchy. Each class uses object as a superclass. All objects, including
arrays) Implement this class of methods.
The object class implements the following methods:
Let's see how the ToString () method is defined in the source code:
Public String toString () { return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ()); }
The following procedure shows that the ToString () method returns the full-qualified name of the class plus the "@" symbol, and finally the hash value of the object, and
The hash value is converted to a hexadecimal output.
In peacetime, we can get the string of the object information through the ToString () method, for example:
Class User { private String name; private int age; Public User (String Name,int age) { this.name=name; this.age=age; } Protected String GetName () { return name; } public void SetName (String name) { this.name = name; }} public class Main {public static void main (string[] args) {User user=new User ("Bill", 25); System.out.println (User.tostring ());}}
Output:
[Email protected]
In addition to using the ToString () method directly, we can overload this method, for example:
Class User { private String name; private int age; Public User (String Name,int age) { this.name=name; this.age=age; } Protected String GetName () { return name; } public void SetName (String name) { this.name = name; } @Override public String toString () { return "name=" +name+ "\nage=" +age; }} public class Main {public static void main (string[] args) {User user=new User ("Bill", 25); System.out.println (User.tostring ());}}
Output:
Name=bill
Age=25
Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43909597 Emotional Control _
Java Learning notes