public class student {private String name;private int age;public student () {super ();} Public student (String name, int age) {super (); this.name = name;this.age = age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} @Override// public string tostring () {// // return super.tostring () ;// // return "Hello";// return "Name:" + name + ", Age:" +  AGE;//&NBSP,} @Overridepublic string tostring () {return "Student [name=" + name + ", age=" + age + "]";}}
/* * public string tostring (): Returns the string representation of the object. A static method under the * * integer class: * public static string tohexstring (int i): Turns an integer into a hexadecimal-represented string * * but this information does not make any sense. Therefore, it is recommended that all subclasses override this method. * How to rewrite it? * all member variable values of the class are returned. The final version of the * rewrite is to automatically generate the ToString () method. * * Note that: * directly outputs the name of an object, which is actually called the ToString () method of the object. */public class studenttest {public static void main (String[] args) {student s = new student (); System.out.println (S.hashcode ()); System.out.println (S.getclass (). GetName ()); System.out.println ("--------------------"); System.out.println (S.tostring ()); System.out.println ("--------------------"); the value of the// tostring () method is equivalent to// getclass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())// this.getclass (). GetName () + ' @ ' +integer.tohexstring ( This.hashcode ()) System.out.println (S.getclass (). GetName () + ' @ ' + integer.tohexstring (S.hashcode ())); System.out.println (S.getclass (). GetName () + "@" + integer.tohexstring (S.hashcode ())); System.out.println (S.tostring ());// the name of the direct output object System.out.println (s);}}
5.3 The ToString () method of the object class