Tutorial_3

「Tutorial_3」の編集履歴(バックアップ)一覧に戻る

Tutorial_3 - (2007/11/18 (日) 10:40:49) の編集履歴(バックアップ)



3 Interaction with Java

3 Javaとの関わり

One of Scala’s strengths is that it makes it very easy to interact with Java code. All classes from the java.lang package are imported by default, while others need to be imported explicitly.
Scala の長所の一つは Java のコードと関わるのが非常に簡単だということです。java.lang パッケージの全てのクラスはデフォルトでインポートされますが、他は明示的にインポートする必要があります。

Let’s look at an example that demonstrates this. We want to obtain and format the current date according to the conventions used in a specific country, say France.
それを示す例をお見せしましょう。現在時刻を取得して特定の国、例えばフランス、で使われる表記方法でフォーマットしたいとしましょう。

Java’s class libraries define powerful utility classes, such as Date and DateFormat. Since Scala interoperates seemlessly with Java, there is no need to implement equivalent classes in the Scala class library–we can simply import the classes of the corresponding Java packages:
Java のクラスライブラリには Date と DateFormat のような強力なユーティリティクラスがあります。Scala は Java と継ぎ目無く協調するので、Scala のクラスライブラリに同様なクラスを実装する必要はありません。単に対応する Java パッケージのクラスをインポート可能です。

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._

object FrenchDate {
  def main(args: Array[String]) {
    val now = new Date
    val df = getDateInstance(LONG, Locale.FRANCE)
    println(df format now)
  }
}

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.
Scala の import 文は Java のと大変似ていますが、ずっと強力です。同じパッケージからの複数のクラスを、一行目の様に波カッコで囲む事によってインポート出来ます。他に違うところは、全てのパッケージあるいはクラスの名前をインポートする時には、アスタリスク (*) の代わりにアンダースコア (_) を使用するという事です。これは、後で示す様にアスタリスクは Scala では有効な識別子 (例えばメソッド名) であるためです。

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.
3行目の import 文は、従って DateFormat クラスの全てのメンバをインポートします。これによって、静的メソッドの getInstance と静的フィールドの LONG を直接見える様にします。

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
mainメソッドの中で最初に、デフォルトで現在時刻を持つJavaのDateクラスのインスタンスを生成します。次に先にインポートした静的なgetInstanceメソッドを用いて日付フォーマットを定義します。最後に、各国語化されたDateFormatインスタンスによってフォーマットされた現在時刻を表示します。最後の行はScalaの構文の興味深い特徴を示しています。引数を一つ取るメソッドは中置き構文を取る事が出来ます。つまりこの式

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.
これは些細な文法規則に見えるかもしれませんが、実は重要な事柄です。詳しくは次の節で述べます。

To conclude this section about integrationwith Java, it should be noted that it is also possible to inherit from Java classes and implement Java interfaces directly in Scala.
Javaとの統合に関するこの節を終えるにあたって、Scalaでは直接にJavaのクラスを継承したりJavaのインターフェイスを実装したりすることが出来るという事も述べておくべきでしょう。

名前:
コメント:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。