Second, Java object-oriented (8) _ inheritance idea - Object class

2018-05-02

A blx programmer is not very good.

 

Object class

 

 

 Check out the API

Basic description:

  1) The Object class is located in the java.lang package. The java.lang package contains the most basic and core classes of Java, which are automatically imported at compile time;

  2) The Object class is the ancestor (root class) of all Java classes. Each class uses Object as a super (parent) class (direct parent or indirect parent). All objects (including arrays) implement the methods of this class. You can use a variable of type Object to point to an object of any type.

 

Why is the Object class the root class of all classes?

Object itself refers to the object, but we found that all objects have some of the same behavior, so it appears as an Object class, representing the object class, and other classes inherit the Object class

 

Reference data types and object classes, including classes, interfaces, and arrays

 

Common methods of Object class:

  • protected void finalize():当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。

                That is, the garbage collector will call this method before reclaiming an object.

  • Class  getClass (): Returns the real type of the current object.
  • int hashCode():  返回该对象的哈希码值。哈希码值决定了对象在哈希码表中的存储位置(内存地址)。
  • boolean equals(Object obj):将当前对象(this)和参数obj做比较

                  a. If the variables on both sides of the operator are of basic data types, then as long as the values ​​of the two variables are equal, the judgment result returns true.

                  b. If the variables on both sides of the operator are both reference data types, then only when the two variables point to the same object (the addresses of the two variables in the memory are the same), the judgment result returns true.

                  equals() and == both compare the memory address of the object.

                  Official recommendation: Every class should override the equals() method, because we care about the data content, not the memory address. For example: two strings, as long as the content is the same, we consider them to be the same object.

  • toString():表示返回该对象的字符串。

          When printing an object, it is actually printing the toString method of the object.

          System.out.println( obj object ); Equivalent to System.out.println( obj object. toString() );

          By default the print object prints the hexadecimal hash code value

          So the official suggestion: we should override the toString() method of each class to return our data.

 

refer to:

http://www.cnblogs.com/mengdd/archive/2013/01/03/2842809.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325201124&siteId=291194637