[英]The java “Object” Class equivalent in Scala
It seems for a good reason I need to switch to Scala! 似乎有充分的理由,我需要切换到Scala! So, excuse me if my question might seem novice.
因此,请问我的问题似乎是新手。
It seems that Any in scala is the supper class of all types and might be considered as an Object type equivalent in Java. 似乎scala中的Any是所有类型的晚餐类,在Java中可能被视为等效的Object类型。 But type Any is abstract, while Object is not.
但是类型Any是抽象的,而Object不是。 More concretely, I can not write the following code in scala (since class Any is abstract and cannot be instantiated):
更具体地说,我无法在scala中编写以下代码(因为Any类是抽象的且无法实例化):
val k:Any = new Any();
while I can write the following code in Java: 虽然我可以用Java编写以下代码:
Object k=new Object();
What should I use if I need a concrete equivalent type of java Object type? 如果我需要具体等效的java对象类型,该怎么办?
AnyRef
is the equivalent of java's Object
. AnyRef
等效于Java的Object
。 (Scala unifies the type system rather than having the object/primitive distinction that Java has. So an instance of Any
might be a descendant of Object
/ AnyRef
, or an instance of a Java primitive type (eg int
). (Or it could also be a "value class" descending from AnyVal
)). (Scala统一类型系统,而不是具有Java具有的对象/原始区分。因此,
Any
的实例可能是Object
/ AnyRef
的后代,或者是Java基本类型的实例(例如int
)。(或者也可以是从AnyVal
的“值类”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.