简体   繁体   English

Scala中的Java“ Object”类等效项

[英]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 type which is not abstract seems to be the equivalent of the Java Object Class (see this ) AnyRef类型,是不是抽象的似乎是Java Object类的当量(见

so the following works in scala: 所以以下在scala中起作用:

  val k:AnyRef = new AnyRef();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM