企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## [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)