/**
* @author Administrator
*
* @description Object class Learning test class
* @history
*/
public class Objecttestdemo {
/**
* @description
* @param args
* @throws ClassNotFoundException
*/
public static void Main (string[] args) throws ClassNotFoundException {
Object class Learning test code
1. ToString Method
2. Equals and Hashcode methods
3. GetClass method
Objecttestdemo OTD = new Objecttestdemo ();
System.out.println (OTD);
System.out.println (Otd.tostring ());//Real call to ToString method
/*
* ToString method in object class
* Public String toString () {
Return GetClass (). GetName () + "@" + integer.tohexstring (Hashcode ());
}
* Equal method in object class
* Public boolean equals (Object obj) {
return (this = = obj);
}*/
Defines the nickname and age attributes, and the equal method returns True if the nickname and age are the same
Objecttestdemo OTD1 = new Objecttestdemo ();
Otd1.nickname = "Eclipse";
Otd1.age = 20;
Objecttestdemo OTD2 = new Objecttestdemo ();
Otd2.nickname = "Eclipse";
Otd2.age = 20;
System.out.println (OTD1==OTD2); False
System.out.println (Otd1.equals (OTD2)); The return is always False if not written.
There is no absolute relationship between the Hashcode method and the Equals method.
Refer to the source code of the note description, in the actual use as far as possible to overwrite both methods, to maintain consistency
Public final native class<?> GetClass ();
The GetClass method is a local method that invokes the underlying code and returns an instance of the class object corresponding to the current object.
Class object instance, which is understood as the corresponding byte-code object in memory, and the Java reflective content is initially learned
Construct an object instance by byte-code object
Class Claz = Otd.getclass ();
Class claz1 = Objecttestdemo.class;
Class claz2 = Class.forName ("Objecttestdemo"); The full path of the class
System.out.println (Otd.getclass ()); Class Objecttestdemo
Only one byte-code object is generated in VM memory
System.out.println (Claz = = CLAZ1); True
}
Method simulation, add two attributes nickname and age
Private String nickname;
private int age;
Note that the parameter type should not be written as Objecttestdemo
public boolean equals (Object obj) {
Objecttestdemo OTD = (objecttestdemo) obj; Transform up, cast
if (this = = OTD) {
return true;
}
if (OTD instanceof Objecttestdemo) {
if (This.nickname.equals (otd.nickname) && this.age = = otd.age) {
return true;
}
}
return false;
}
Public String toString () {
return "HelloWorld"; Overwrite ToString method, return HelloWorld
}
}
Dark Horse Programmer-----Object class and wrapper class