专栏首页一个会写诗的程序员的博客Koltin Any 类型Koltin Any 类型

Koltin Any 类型Koltin Any 类型

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
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Kotlin 函数编程详解函数Kotlin 开发者社区

    Functions in Kotlin are declared using the fun keyword:

    一个会写诗的程序员
  • Kotlin 编码规约

    如需根据本风格指南配置 IntelliJ 格式化程序,请安装 Kotlin 插件1.2.20 或更高版本,转到“Settings | Editor | Code...

    一个会写诗的程序员
  • Java 15 的新特性: TextBlocks

    JDK 15 is the open-source reference implementation of version 15 of the Java SE ...

    一个会写诗的程序员
  • 更坏的WER,但更好的BLEU?在多任务端到端语音翻译中利用单词嵌入作为中介(CS CL)

    语音翻译(ST)旨在学习从源语言中的语音到目标语言中的文本的转换。先前的工作表明,多任务学习提高了ST性能,其中识别解码器生成源语言的文本,翻译解码器根据识别解...

    刘子蔚
  • Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts

    the i-th necklace has a brightness ai, where all the ai are pairwise distinct (i...

    风骨散人Chiam
  • 楼宇自动化系统中基于模糊巡逻机器人的开发(CS AI)

    楼宇自动化系统(BAS)具有监视和控制所有楼宇子系统运行的功能,如暖通空调(暖通空调控制)、用电管理、火警控制、保安和门禁控制以及电器开关控制。在BAS中,几乎...

    小童
  • Android LitePal 让数据使用so easy(原文)

    LitePal is an open source Android library that allows developers to use SQLite d...

    Vance大飞
  • Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000...

    ShenduCC
  • Flink安装

    前提:安装:已安好hadoop,环境已经配好 java 7.X 及其以上 scala有对应版本 单机: 下载、解压、 需要注意:flink与hadoop版...

    shengjk1
  • SAP WebClient UI界面元素ID生成的逻辑分析

    If we open a WebClient UI page with Chrome and inspect its UI element via Chrome...

    Jerry Wang

扫码关注云+社区

领取腾讯云代金券