A common class, before rewriting the toString function, outputs its objects directly. The results are as follows:
package blogTest;
class Test{
public int age;
public double acount;
public String string;
public void setAge(int age) {
this.age = age;
}
public void setAcount(double acount) {
this.acount = acount;
}
public void setString(String string) {
this.string = string;
}
public int getAge() {
return age;
}
public double getAcount() {
return acount;
}
public String getString() {
return string;
}
}
public class ToString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
test.setAge(10);
test.setAcount(20.0);
test.setString("hello wolrd!");
System.out.println("test:\t\t\t"+test);//blogTest.Test@2a139a55
System.out.println("test.toString():\t"+test.toString());//Unrewritten toString:blogTest.Test@2a139a55
}
}
/*
The results are as follows:
test: blogTest.Test@2a139a55
test.toString(): blogTest.Test@2a139a55
*/
Note:
When you want to output an object. The toString method that calls the object by default.
Each class inherits Object objects by default, and the toString method source code in it is as follows:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
getClass().getName() gets the class name for reflection
hashCode() is the local method that returns the address value of the object.
So the output is shown in the figure above.
After overriding the toStirng method of this class
package blogTest;
class Test{
public int age;
public double acount;
public String string;
public void setAge(int age) {
this.age = age;
}
public void setAcount(double acount) {
this.acount = acount;
}
public void setString(String string) {
this.string = string;
}
public int getAge() {
return age;
}
public double getAcount() {
return acount;
}
public String getString() {
return string;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Age:"+age+"\t Balance:"+acount+"\t Name:"+string;
}
}
public class ToString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
test.setAge(10);
test.setAcount(20.0);
test.setString("hello wolrd!");
System.out.println("test:\t\t\t"+test);//blogTest.Test@2a139a55
System.out.println("test.toString():\t"+test.toString());//Rewrite toString:blogTest.Test@2a139a55
}
}
/*
The results are as follows:
test: Age: 10 balance: 20.0 name: hello wolrd!
test.toString(): Age: 10 balance: 20.0 name: hello wolrd!
*/
In addition, we can write the toString() method in different ways, which has been given a form of returning strings directly, and the following two are given.
———— Method 1——-
Using the StringBuffer class
package blogTest;
class Test{
public int age;
public double acount;
public String string;
public void setAge(int age) {
this.age = age;
}
public void setAcount(double acount) {
this.acount = acount;
}
public void setString(String string) {
this.string = string;
}
public int getAge() {
return age;
}
public double getAcount() {
return acount;
}
public String getString() {
return string;
}
@Override
public String toString() {
// TODO Auto-generated method stub
// return "Age:"+age+"t balance:"+acount+"t name:"+string;
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("Age:"+age);
stringBuffer.append("\t balance:"+acount);
stringBuffer.append("\t Name:"+string);
return stringBuffer.toString();
}
}
public class ToString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
test.setAge(10);
test.setAcount(20.0);
test.setString("hello wolrd!");
System.out.println("test:\t\t\t"+test);//blogTest.Test@2a139a55
System.out.println("test.toString():\t"+test.toString());//Rewrite toString:blogTest.Test@2a139a55
}
}
/*
The results are as follows:
test: Age: 10 balance: 20.0 name: hello wolrd!
test.toString(): Age: 10 balance: 20.0 name: hello wolrd!
*/
———— Method 2——
The toString method is rewritten by reflection.
package blogTest;
import java.lang.reflect.Field;
class Test{
public int age;
public double acount;
public String string;
public void setAge(int age) {
this.age = age;
}
public void setAcount(double acount) {
this.acount = acount;
}
public void setString(String string) {
this.string = string;
}
public int getAge() {
return age;
}
public double getAcount() {
return acount;
}
public String getString() {
return string;
}
public void test() {
Class clazz=this.getClass(); // Get the class object of this class
Field[] fields=clazz.getDeclaredFields(); //Get all member variables of this class
System.out.println("Output member variables of this class:");
for (Field field : fields) {
// System.out.println(field);
/*
* public int blogTest.Test.age
public double blogTest.Test.acount
public java.lang.String blogTest.Test.string
*/
try {
System.out.println(field.getName()+":"+field.get(this));
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*age
acount
string*/
}
}
@Override
public String toString() {
Class clazz=this.getClass(); // Get the class object of this class
StringBuffer stringBuffer=new StringBuffer();
Field[] fields=clazz.getDeclaredFields(); //Get all member variables of this class
for (Field field : fields) {
try {
stringBuffer.append(field.getName()+":"+field.get(this)+"\t");
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//for
return stringBuffer.toString();
}//toString
}//Don't forget this!!!
/*
* The method main cannot be declared static; static methods can only be declared in a static or top level type
*/
public class ToString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=new Test();
test.setAge(10);
test.setAcount(20.0);
test.setString("hello wolrd!");
System.out.println("test:\t\t\t"+test);//blogTest.Test@2a139a55
System.out.println("test.toString():\t"+test.toString());//Rewrite toString:blogTest.Test@2a139a55
// test.test();
}
}
/*
Output results:
test: age:10 acount:20.0 string:hello wolrd!
test.toString(): age:10 acount:20.0 string:hello wolrd!
*/
Note: The test method in it forgot how to use reflection to get the member variable name and the member variable value. It can be used to try, but you can ignore it.
Summary:
Here we can see that rewriting toString with reflection is the most troublesome method, but there is no need to rewrite it if a new member variable is added.
However, it seems that it is not good to use reflection to obtain member variables or member methods, which violates the closeness of classes. ()
Here's an explanation: This is a simple annotated one.
Class clazz=this.getClass(); // Get the class object of this class
StringBuffer stringBuffer=new StringBuffer();
Field[] fields=clazz.getDeclaredFields(); //Get all member variables of this class
This is mainly to traverse member variables
field.getName() can get the name of the member variable;
field.get(this) gets the value of the member variable here
Here we use the StringBuffer class we used earlier to string it together.
Be careful to throw an exception.
for (Field field : fields) {
try {
stringBuffer.append(field.getName()+":"+field.get(this)+"\t");
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//for
Note:
Well, I made a silly mistake when I wrote here. The main () function reported a mistake here.
The method main cannot be declared static; static methods can only be declared in a static or top level type
Best thinking is puzzled.
Finally, it was discovered that the brackets on the right of {} of the upper class class class were carelessly commented out when the modification was made, but the brackets were originally missing.