Koltin Any 类型

简介: Koltin Any 类型kotlin.AnyThe root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.

Koltin Any 类型

kotlin.Any

The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.

All classes in Kotlin have a common ultimate superclass kotlin.Any. kotlin.Any is the default superclass for a class with no superclass declared. Any is not java.lang.Object, in particular, it does not have any members other than methods equals(), hashCode() and toString(). It is the root on the hierarchy of all non-nullable types. To declare a variable that can store a reference to a value of any type (nullable or not), the nullable type kotlin.Any? can be used.

All interfaces are considered to be subtypes of kotlin.Any.

kotlin.Any has the following parts:

public constructor()
public open fun equals(other: Any?): Boolean
public open fun hashCode(): Int
public open fun toString(): String

The constructor implementation is an empty block that does nothing.

The default implementation of the method equals performs reference equality check. It can be overridden in derived types to implement a custom equality behavior. It is strongly recommended that any implementation of this method satisfies the following rules:

Termination. The method shall complete normally (i.e. it shall not go into an infinite loop or throw an exception).

Purity. An invocation x.equals(y) shall not change any observable state of objects x and y (it may update some caches though).

Reproducibility. Multiple invocations x.equals(y) shall return the same result (unless state of either x or y has changed in between).

Reflexivity. For any non-null value x the expression x.equals(x) shall return true.

Symmetry. For any non-values x and y expressions x.equals(y) and y.equals(x) shall return the same value (unless state of either x or y has changed in between).

Transitivity. Invocations x.equals(y) and x.equals(z) shall return the same value if y.equals(z) returns true (unless state of either x, y or z has changed in between).

[Note: Many functions from the standard library may produce unexpected results if any of these rules is violated. It is reasonable to expect implementations of the equals method to be conformant to these rules when writing library code, but one should not rely on it when reasoning about code security and other critical properties, because a malicious client could exploit it and create a security breach. End note]

The default implementation of the hashCode method returns a hash code for the current object, consistent with the behavior of the equals objects (i.e. equal objects always have the same hash code).

TODO: toString method

Any is an open non-abstract class.

Kotlin compiler treats kotlin.Any and java.lang.Object as two different types, but in runtime they are represented with the samejava.lang.Objectclass.

javaClass property returns the runtime class of an instance, so that's why you get the same java.lang.Object class in both cases.

There are also other types which are different in compile time, but same in runtime, they are listed in the Mapped types section of the documentation.

Kotlin treats some Java types specially. Such types are not loaded from Java “as is”, but are mapped to corresponding Kotlin types. The mapping only matters at compile time, the runtime representation remains unchanged. Java’s primitive types are mapped to corresponding Kotlin types (keeping platform types in mind)

/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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 reference value x, x.equals(x) should return true.
     * * Symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
     * * Transitive:  for any non-null reference 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 reference 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.
     *
     * Note that the `==` operator in Kotlin code is translated into a call to [equals] when objects on both sides of the
     * operator are not null.
     */
    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
}

相关文章
|
6月前
|
传感器 人工智能 图形学
UnityAI——常用感知类型的实现
UnityAI——常用感知类型的实现
|
3月前
|
Kubernetes 负载均衡 网络协议
在k8S中,Servic类型有哪些?
在k8S中,Servic类型有哪些?
|
5月前
|
编译器 程序员 语音技术
C++的超20种函数类型分享
C++超20种函数类型:编程语言规定规则,编译器实现预定规则
|
程序员 数据库
软件文档的类型有哪些?
软件文档的类型有哪些?
206 0
|
6月前
|
JavaScript 前端开发 编译器
TypeScript 中的基础类型:原始类型、对象类型、数组类型、元组类型、枚举类型和联合类型
TypeScript 中的基础类型:原始类型、对象类型、数组类型、元组类型、枚举类型和联合类型
74 1
|
JavaScript 前端开发
比较不同的类型
比较不同的类型
93 0
|
JSON JavaScript C语言
转换类型的那些事儿
转换类型的那些事儿
117 0
判断Object中数据类型(已知类型、未知类型))
判断Object中数据类型(已知类型、未知类型))
137 0
|
Go
类型
类型
172 0
下一篇
无影云桌面