As we know, there are subclasses and parent classes in Java. Subclasses are formed by inheriting the parent class. So are there any parent classes in the parent class? The answer is that the parent class's parent class is the object class, and all parent classes inherit it. Then, according to the inherited attributes, each subclass has an object class. However, we do not inherit it in a purpose, but inherit it is a purpose. We need to use the methods defined in the object. There are many methods defined in the object. For details, refer to the API help document. Below I will introduce two methods in the object. The inherited methods often need to be rewritten.
First, for example, the comparison method is used to compare whether the addresses of two objects are equal. The actual comparison address is a hash address, which is often not available in development. We use more content in the comparison object, such as whether the data members are the same. Since it inherits the object, we can rewrite it and overwrite the original object method. Generally, this is done. Let's see the specific code below. It also involves the knowledge of upward transformation and downward transformation. For details, please read my previous podcast. There is also the tostring() method that returns the class name + hash value
/*Object: is the direct latter indirect parent class of all objects, the legendary God. What is defined in this class is definitely the functions that all objects have. A comparison method for whether the object is the same has been provided in the Object class. If there are similar functions in the custom class, there is no need to redefine it. Just follow the functions in the parent class and create your own unique comparison content. This is coverage. */class Demo //extends Object{ private int num; Demo(int num) { this.num = num; } public boolean equals(Object obj)//Object obj = new Demo(); { if(!(obj instanceof Demo)) return false; Demo d = (Demo)obj; return this.num == d.num; } /* public boolean compare(Demo d) { return this.num==d.num; } */ public String toString() { return "demo:"+num; }}class Person {}class ObjectDemo { public static void main(String[] args) { Demo d1 = new Demo(4); System.out.println(d1);// When the output statement prints the object, the object's toString method will be automatically called. Print the string representation of the object. Demo d2 = new Demo(7); System.out.println(d2.toString()); //Demo d2 = new Demo(5); //Class c = d1.getClass();/// System.out.println(c.getName());// System.out.println(c.getName()+"@@"+Integer.toHexString(d1.hashCode()));// System.out.println(d1.toString()); //Person p = new Person(); ///System.out.println(d1.equals(p)); }}The above detailed explanation of the simple use of java_object is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.