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 条评论
登录 后参与评论

相关文章

来自专栏函数式编程语言及工具

Akka(34): Http:Unmarshalling,from Json

  Unmarshalling是Akka-http内把网上可传输格式的数据转变成程序高级结构话数据的过程,比如把Json数据转换成某个自定义类型的实例。按具体流...

24970
来自专栏Spring相关

Springboot+ajax传输json数组以及单条数据的方法

47320
来自专栏码匠的流水账

聊聊rocketmq的FileAppender

org/apache/rocketmq/logging/inner/LoggingBuilder.java

8910
来自专栏码匠的流水账

聊聊resilience4j的CircuitBreakerStateMachine

本文主要研究一下resilience4j的CircuitBreakerStateMachine

15120
来自专栏绿巨人专栏

BookNote: Refactoring - Improving the Design of Existing Code

31260
来自专栏菩提树下的杨过

mybatis3.2.8 与 hibernate4.3.6 混用

mybatis、hibernate这二个框架各有特色,对于复杂的查询,利用mybatis直接手写sql控制起来更灵活,而一般的insert/update,hib...

22960
来自专栏算法修养

HOJ 2091 Chess(三维简单DP)

Chess My Tags (Edit) Source : Univ. of Alberta Local Contest 1999.10.16 ...

389100
来自专栏小樱的经验随笔

HDU 1039 Easier Done Than Said?

Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 6...

29760
来自专栏nummy

《eloquent javascript》 notes1

There is only one value in JavaScript that is not equal to itself, and that is N...

10330
来自专栏冷冷

MongoDB 整合spring-data-jpa,spring 4.x

Spring Data提供了repository 抽象方式,可以极大的减少数据访问层千篇一律的类似的重复的代码。 基本DAO都会实现,find,findAll,...

1.1K100

扫码关注云+社区

领取腾讯云代金券