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.Object
class.
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
}