如何从Scala访问Java静态方法

如何从Scala访问Java静态方法

本文介绍了如何从Scala访问Java静态方法,给出它所驻留的类的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

给定type-alias 类型Cal = java.util.Calendar static <$ c $怎么样c> getInstance 方法可以访问吗?我在Scala REPL中尝试了以下内容:

Given the type-alias type Cal = java.util.Calendar how can the static getInstance method be accessed? I tried the following in Scala REPL:

scala> type Cal = java.util.Calendar
defined type alias Cal
scala> Cal.getInstance
<console>:8: error: not found: value Cal
          Cal.getInstance
          ^
scala> val Cal = java.util.Calendar
<console>:7: error: object Calendar is not a value
   val Cal = java.util.Calendar
                       ^

导入java.util。{Calendar => Cal} 后跟导入Cal ._ 真的是我最好的选择吗?

Is import java.util.{Calendar => Cal} followed by import Cal._ really my best bet?

推荐答案

你做不到。

是, import java.util。{Calendar => Cal} 真的是你最好的选择。

Yes, import java.util.{Calendar => Cal} is really your best bet.

从概念上讲,Scala object 成员类似于Java类的静态成员。这可能表明以下方法可行,但事实并非如此,因为实际上没有可用于Java类的单例对象。

Conceptually, Scala object members are similar to static members of Java classes. This might suggest the following would work, but it doesn't since there are actually no singleton objects available for Java classes.

scala> val Cal = java.util.Calendar
<console>:13: error: object Calendar is not a value
       val Cal = java.util.Calendar
                           ^

这篇关于如何从Scala访问Java静态方法,给出它所驻留的类的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:38