http://www.scala-lang.org/api/current/index.html#scala.Any
个人学习笔记,本人初学编程语言,力争第一遍能了解scala基本库中的API的作用并发现不理解的问题,争取再深入学习时能够有针对性的进行研究和学习。水平有限难免会有理解错误。如有读者发现错误或者有问题可以留言指教。希望大家不吝赐教!文中会引用他人的博客的连接,如遇版权问题请联系我,立刻纠正。感谢前辈们的宝贵总结。
1. Any类的简述
Class Any
is therootof the Scala class hierarchy. Every class in a Scala execution environment inherits directly or indirectly from this class.
Starting with Scala 2.10 it is possible to directly extend Any
using universal traits. A universal trait is a trait that extends Any
, only has def
s as members, and does no initialization.
The main use case for universal traits is to allow basic inheritance of methods for value classes. For example,
trait Printable extends Any { def print(): Unit = println(this) } class Wrapper(val underlying: Int) extends AnyVal with Printable val w = new Wrapper(3) w.print()
See the Value Classes and Universal Traits for more details on the interplay of universal traits and value classes.
Any类是scala中其它类的root(根),其它的类都直接或者间接的继承自Any类。从2.10版本开始,可以直接的使用universal traits来扩展(extends)Any类。一个universal traits仅仅含有使用def定义的成员(方法成员),没有value(值成员),没有初始化操作。def在scala中用来定义函数或者方法。
universal traits的主要用例就是用来继承value classes(我的理解就是从class里面继承value:也就是java中的成员变量)。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. 类成员
Abstract Value Members
Concrete Value Members
-
final def!=(arg0: Any): Boolean
Test two objects for inequality.
-
final def##(): Int
Equivalent to
x.hashCode
except for boxed numeric types andnull
. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. Fornull
returns a hashcode wherenull.hashCode
throws aNullPointerException
. -
final def==(arg0: Any): Boolean
Test two objects for equality. The expression
x == that
is equivalent toif (x eq null) that eq null else x.equals(that)
. -
final defasInstanceOf[T0]: T0
Cast the receiver object to be of type
T0
.Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression
1.asInstanceOf[String]
will throw aClassCastException
at runtime, while the expressionList(1).asInstanceOf[List[String]]
will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type. -
defequals(arg0: Any): Boolean
Compares the receiver object (
this
) with the argument object (that
) for equivalence.Any implementation of this method should be an equivalence relation:
- It is reflexive: for any instance
x
of typeAny
,x.equals(x)
should returntrue
. - It is symmetric: for any instances
x
andy
of typeAny
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any instances
x
,y
, andz
of typeAny
ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
.
If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is usually necessary to override
hashCode
to ensure that objects which are "equal" (o1.equals(o2)
returnstrue
) hash to the same scala.Int. (o1.hashCode.equals(o2.hashCode)
). - It is reflexive: for any instance
-
defhashCode(): Int
Calculate a hash code value for the object.
The default hashing algorithm is platform dependent.
Note that it is allowed for two objects to have identical hash codes (
o1.hashCode.equals(o2.hashCode)
) yet not be equal (o1.equals(o2)
returnsfalse
). A degenerate implementation could always return0
. However, it is required that if two objects are equal (o1.equals(o2)
returnstrue
) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)
). Therefore, when overriding this method, be sure to verify that the behavior is consistent with theequals
method. -
final defisInstanceOf[T0]: Boolean
Test whether the dynamic type of the receiver object is
T0
.Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression
1.isInstanceOf[String]
will returnfalse
, while the expressionList(1).isInstanceOf[List[String]]
will returntrue
. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type. -
deftoString(): String
Returns a string representation of the object.
The default representation is platform dependent.
Returns the runtime class representation of the object.
returns-
-
a class object corresponding to the runtime type of the receiver.
Any类的抽象成员,返回值是这个class对象在运行时的相应的类的类型。