加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Scala中创建AnyRef的现有Java类子类?

发布时间:2020-12-16 18:21:24 所属栏目:安全 来源:网络整理
导读:Scala继承层次结构图: Scala for the Impatient中的上图显示了所有Java类都是AnyRef类的子类. Figure 8–1 shows the inheritance hierarchy of Scala classes. The classes that correspond to the primitive types in Java,as well as the type Unit ,ext
Scala继承层次结构图:

Scala for the Impatient中的上图显示了所有Java类都是AnyRef类的子类.

Figure 8–1 shows the inheritance hierarchy of Scala classes. The classes that correspond to the primitive types in Java,as well as the type Unit,extend AnyVal. All other classes are subclasses of the AnyRef class,which is a synonym for the Object class from the Java or .NET virtual machine.
Both AnyVal and AnyRef extend the Any class,the root of the hierarchy.

如何在不改变Java类的情况下实现这一目标?它是在字节码级别处理的吗?

解决方法

免责声明:我绝不是Scala专家,但拥有20年的Java经验,包括JVM.

我的解释是那句话

All other classes are subclasses of the AnyRef class,which is a
synonym for the Object class from the Java
or .NET virtual machine.

(强调我的)完美地解释了它.

对于JVM,没有AnyRef类.该文本将AnyRef称为同义词,而JVM没有这样的概念.因此,Scala编译器可能会用java.lang.Object替换AnyRef的出现.当然,所有Java类都是java.lang.Object的子类,它只是在Scala编译器中得到一个新名称,因此它们都是从AnyRef继承的(a.k.a.Object)

我打赌你不会在任何Scala运行时JAR库中找到AnyRef.class条目,这意味着对于JVM,类似AnyRef的类不存在.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读