<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                以下為 Object 中的通用方法 ```java public final native Class<?> getClass() public native int hashCode() public boolean equals(Object obj) protected native Object clone() throws CloneNotSupportedException public String toString() public final native void notify() public final native void notifyAll() public final native void wait(long timeout) throws InterruptedException public final void wait(long timeout, int nanos) throws InterruptedException public final void wait() throws InterruptedException protected void finalize() throws Throwable {} // JVM內存回收之finalize()方法 ``` ## equals() **1. equals() 與 == 的區別** - 對于基本類型,== 判斷兩個值是否相等,基本類型沒有 equals() 方法。 - 對于引用類型,== 判斷兩個實例是否引用同一個對象,而 equals() 判斷引用的對象是否等價。 ```java Integer x = new Integer(1); Integer y = new Integer(1); System.out.println(x.equals(y)); // true System.out.println(x == y); // false ``` **2. 等價關系** (一)自反性 ```java x.equals(x); // true ``` (二)對稱性 ```java x.equals(y) == y.equals(x); // true ``` (三)傳遞性 ```java if (x.equals(y) && y.equals(z)) x.equals(z); // true; ``` (四)一致性 多次調用 equals() 方法結果不變 ```java x.equals(y) == x.equals(y); // true ``` (五)與 null 的比較 對任何不是 null 的對象 x 調用 x.equals(null) 結果都為 false ```java x.euqals(null); // false; ``` **3. 實現** - 檢查是否為同一個對象的引用,如果是直接返回 true; - 檢查是否是同一個類型,如果不是,直接返回 false; - 將 Object 實例進行轉型; - 判斷每個關鍵域是否相等。 ```java public class EqualExample { private int x; private int y; private int z; public EqualExample(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EqualExample that = (EqualExample) o; if (x != that.x) return false; if (y != that.y) return false; return z == that.z; } } ``` ## hashCode()   hasCode() 返回散列值,而 equals() 是用來判斷兩個實例是否等價。**等價的兩個實例散列值一定要相同,但是散列值相同的兩個實例不一定等價。**   在覆蓋 equals() 方法時應當總是覆蓋 hashCode() 方法,保證等價的兩個實例散列值也相等。   下面的代碼中,新建了兩個等價的實例,并將它們添加到 HashSet 中。我們希望將這兩個實例當成一樣的,只在集合中添加一個實例,但是因為 EqualExample 沒有實現 hasCode() 方法,因此這兩個實例的散列值是不同的,最終導致集合添加了兩個等價的實例。 ```java EqualExample e1 = new EqualExample(1, 1, 1); EqualExample e2 = new EqualExample(1, 1, 1); System.out.println(e1.equals(e2)); // true HashSet<EqualExample> set = new HashSet<>(); set.add(e1); set.add(e2); System.out.println(set.size()); // 2 ```   理想的散列函數應當具有均勻性,即不相等的實例應當均勻分布到所有可能的散列值上。這就要求了散列函數要把所有域的值都考慮進來,可以將每個域都當成 R 進制的某一位,然后組成一個 R 進制的整數。R 一般取 31,因為它是一個奇素數,如果是偶數的話,當出現乘法溢出,信息就會丟失,因為與 2 相乘相當于向左移一位。   一個數與 31 相乘可以轉換成移位和減法:`31\*x == (x<<5)-x`,編譯器會自動進行這個優化。 ```java @Override public int hashCode() { int result = 17; result = 31 * result + x; result = 31 * result + y; result = 31 * result + z; return result; } ``` ## toString() 默認返回 ToStringExample@4554617c 這種形式,其中 @ 后面的數值為**散列碼的無符號十六進制**表示。 ```java public class ToStringExample { private int number; public ToStringExample(int number) { this.number = number; } } ``` ```java ToStringExample example = new ToStringExample(123); System.out.println(example.toString()); ``` ```html ToStringExample@4554617c ``` ## clone() **1. cloneable** clone() 是 Object 的 protect 方法,它不是 public,一個類不顯式去重寫 clone(),其它類就不能直接去調用該類實例的 clone() 方法。 ```java public class CloneExample { private int a; private int b; } ``` ```java CloneExample e1 = new CloneExample(); // CloneExample e2 = e1.clone(); // 'clone()' has protected access in 'java.lang.Object' ``` 重寫 clone() 得到以下實現: ```java public class CloneExample { private int a; private int b; @Override protected CloneExample clone() throws CloneNotSupportedException { return (CloneExample)super.clone(); } } ``` ```java CloneExample e1 = new CloneExample(); try { CloneExample e2 = e1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } ``` ```html java.lang.CloneNotSupportedException: CloneTest ``` 以上拋出了 CloneNotSupportedException,這是因為 CloneTest 沒有實現 Cloneable 接口。 ```java public class CloneExample implements Cloneable { private int a; private int b; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } ``` 應該注意的是,clone() 方法并不是 Cloneable 接口的方法,而是 Object 的一個 protected 方法。Cloneable 接口只是規定,如果一個類沒有實現 Cloneable 接口又調用了 clone() 方法,就會拋出 CloneNotSupportedException。 參考資料: - [【必讀】搞懂 Java equals 和 hashCode 方法 - 掘金](https://juejin.im/post/5ac4d8abf265da23a4050ae3)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看