![](https://csdnimg.cn/release/download_crawler_static/10863579/bg5.jpg)
Scala’s import statement looks very similar to Java’s equivalent, however, it is
more powerful. Multiple classes can be imported from the same package by
enclosing them in curly braces as on the first line. Another difference is that when
importing all the names of a package or class, one uses the underscore character
(
_) instead of the asterisk (*). That’s because the asterisk is a valid Scala identifier
(e.g. method name), as we will see later.
因此,上例中第三行的 import 语句,引入了 DateFormat 类的所有成员,这样该类的静
态方法
getDateInstance 和静态属性 LONG 对 FrenchDate 类来说,是直接可见的(directly
visible)。
The import statement on the third line therefore imports all members of the
DateFormat class. This makes the static method getDateInstance and the static field
LONG directly visible.
在 main 方法中,我们首先创建一个 Java 的 Date 实例,该实例默认取得系统当前时
间;接下来,我们使用从
DateFormat 类中引入的静态方法 getDateInstance 创建一
个负责日期格式化的对象 df ,创建过程中,通过参数指定了本地化区域
(
Locale.FRANCE);最后,使用 df 将当前时间进行格式化并打印输出到控制台。
这个方法的最后一行,体现了 Scala 语法中一种很有意思的特性(interesting
property):如果一个方法只接受一个参数,那么可以使用 infix 语法,也就是说,
下面的表达式:
df format now
和 df.format(now)的语义完全相同,只是前者更加简洁。
Inside the
main method we first create an instance of Java’s Date class which by
default contains the current date. Next, we define a date format using the static
getDateInstance method that we imported previously. Finally, we print the current
date formatted according to the localized
DateFormat instance. This last line shows
an interesting property of Scala’s syntax. Methods taking one argument can be
used with an infix syntax. That is, the expression
df format now
is just another, slightly less verbose way of writing the expression
df.format(now)
这虽然是一个很小的语法细节,但它具有深远的影响,本文后续的章节中将会有进
一步的论述。
This might seem like a minor syntactic detail, but it has important consequences,
one of which will be explored in the next section.
最后,从 Java 与 Scala 整合的角度看,值得一提的是,Scala 中可以直接继承 Java
的类或者实现 Java 的接口。