<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## [kotlin.Any](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin--any-index.html?lang=en) 和Object 作為Java 類層級結構的根差不多,**Any 類型是Kotlin 所有非空類型的超類型(非空類型的根),Any在運行時,其類型自動映射成 java.lang.Object**。但是在Java 中, Object 只是所有引用類型的超類型(引用類型的根) ,而基本數據類型并不是類層級結構的一部分。這意味著當你需要Object 的時候,不得不使用像`java.lang.Integer`這樣的包裝類型來表示基本數據類型的值。而在Kotlin 中, Any是所有類型的超類型(所有類型的根),包括像Int 這樣的基本數據類型。 和Java 一樣,把基本數據類型的值賦給Any 類型的變量時會自動裝箱: ``` val answer: Any = 42//Any是引用類型,所以值42會被裝箱 ``` >[info]【注意】**Any 是非空類型**,所以**Any 類型的變量不可以持有null 值**。在Kotlin中如果你需要可以持有任何可能值的變量,包括null 在內,必須使用Any?類型。 在底層, Any 類型對應`java.lang.Object`。Kotlin 把Java 方法參數和返回類型中用到的Object 類型看作Any (更確切地是當作平臺類型,因為其可空性是未知的)。當Kotlin 函數使用Any時,它會被編譯成Java 字節碼中的Object 。 正如你在第4 章中看到的,所有Kotlin 類都包含下面三個方法:`toString`、`equals` 和`hashCode` 。這些方法都繼承自Any。Any并不能使用其他`java.lang.Object` 的方法(比如wait 和notify),但是可以通過手動轉換成` java.lang.Object`來調用這些方法。 kotlin的源碼 ~~~ package kotlin /** * The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass. */ public open class Any { /** * Indicates whether some other object is "equal to" this one. Implementations must fulfil the following * requirements: * * * Reflexive: for any non-null value `x`, `x.equals(x)` should return true. * * Symmetric: for any non-null values `x` and `y`, `x.equals(y)` should return true if and only if `y.equals(x)` returns true. * * Transitive: for any non-null values `x`, `y`, and `z`, if `x.equals(y)` returns true and `y.equals(z)` returns true, then `x.equals(z)` should return true. * * Consistent: for any non-null values `x` and `y`, multiple invocations of `x.equals(y)` consistently return true or consistently return false, provided no information used in `equals` comparisons on the objects is modified. * * Never equal to null: for any non-null value `x`, `x.equals(null)` should return false. * * Read more about [equality](https://kotlinlang.org/docs/reference/equality.html) in Kotlin. */ public open operator fun equals(other: Any?): Boolean /** * Returns a hash code value for the object. The general contract of `hashCode` is: * * * Whenever it is invoked on the same object more than once, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified. * * If two objects are equal according to the `equals()` method, then calling the `hashCode` method on each of the two objects must produce the same integer result. */ public open fun hashCode(): Int /** * Returns a string representation of the object. */ public open fun toString(): String } ~~~ ### **對象相等性** 從Any的源碼注釋中,我們可以看到,判斷兩個對象是否相等,需要滿足以下條件: * 自反性:對于任何非空引用值x,x.equals(x)應返回true。 * 對稱性:對于任何非空引用值x和y,x.equals(y)應返回true當且僅當y.equals(x)返回true。 * 傳遞性:對于任何非空引用值x,y,z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)應返回true * 一致性:對于任何非空引用值x和y,多次調用x.equals(y)始終返回true或者始終返回false。 另外,在Kotlin中,操作符 == 會被編譯器翻譯成調用 equals() 函數。 Kotlin的基本類型的類圖結構如下圖所示,大圖[點擊這里](https://box.kancloud.cn/7efb38bb917f14274fa8669ccbd4998b_1186x908.png) ![](https://box.kancloud.cn/7efb38bb917f14274fa8669ccbd4998b_1186x908.png)
                  <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>

                              哎呀哎呀视频在线观看