Tutorial_3

「Tutorial_3」の編集履歴(バックアップ)一覧はこちら

Tutorial_3」(2010/10/21 (木) 13:15:39) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

[[トップ>トップページ]] > [[チュートリアル和訳>Tutorial和訳]] > [[3 Interaction with Java>Tutorial_3]] #co(){*3 Interaction with 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. } *3 Javaとの連携 Scala の長所の一つは Java コードとの連携が非常に簡単だということです。java.lang パッケージの全てのクラスはデフォルトでインポートされます。その他は明示的にインポートする必要があります。 #co(){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. } それを示す例をお見せしましょう。現在時刻を取得して特定の国、例えばフランス*1で使われる表記方法を適用したいとしましょう。 #co(){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) } } #co(){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 では有効な識別子 (例えばメソッド名) だからです。 #co(){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 クラスの全てのメンバをインポートします。これによって静的メソッド getDateInstance と静的フィールド LONG を直接見えるようにします。 #co(){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 クラスのインスタンスを生成します。次に、先にインポートした静的メソッド getDateInstance を用いて日付フォーマットを定義します。最後に、各国語化された DateFormat インスタンスによってフォーマットされた現在時刻を表示します。最後の行は Scala 構文の興味深い特徴を示しています。引数を一つ取るメソッドには中置構文を使えます。つまりこの df format now #co(){is just another, slightly less verbose way of writing the expression } という式は、次の式の文字数の少ない表記方法だといえます。 df.format(now) #co(){This might seem like a minor syntactic detail, but it has important consequences, one of which will be explored in the next section. } これは些細な文法規則に見えるかもしれませんが、実は重要な事です。詳しくは次節で述べます。 #co(){To conclude this section about integration with 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 のインターフェイスを実装したりできる、という事も述べておくべきでしょう。 #co(){Other regions such as the french speaking part of Switzerland use the same covnetions.} #hr(width=80%)  *1 例えばスイスでフランス語を話す地域など、他の地方でも同じ記法が使用されます。 #center(){[[前ページ>>Tutorial_2]]  [[目次>>Tutorial和訳]]  [[次ページ>>Tutoarial_4]]} ---- #comment()
[[トップ>トップページ]] > [[チュートリアル和訳>Tutorial和訳]] > [[3 Interaction with Java>Tutorial_3]] #co(){*3 Interaction with 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. } *3 Javaとの連携 Scala の長所の一つは Java コードとの連携が非常に簡単だということです。java.lang パッケージの全てのクラスはデフォルトでインポートされます。その他は明示的にインポートする必要があります。 #co(){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. } それを示す例をお見せしましょう。現在時刻を取得して特定の国、例えばフランス*1で使われる表記方法を適用したいとしましょう。 #co(){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) } } #co(){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 では有効な識別子 (例えばメソッド名) だからです。 #co(){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 クラスの全てのメンバをインポートします。これによって静的メソッド getDateInstance と静的フィールド LONG を直接見えるようにします。 #co(){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 クラスのインスタンスを生成します。次に、先にインポートした静的メソッド getDateInstance を用いて日付フォーマットを定義します。最後に、各国語化された DateFormat インスタンスによってフォーマットされた現在時刻を表示します。最後の行は Scala 構文の興味深い特徴を示しています。引数を一つ取るメソッドには中置構文を使えます。つまりこの df format now #co(){is just another, slightly less verbose way of writing the expression } という式は、次の式の文字数の少ない表記方法だといえます。 df.format(now) #co(){This might seem like a minor syntactic detail, but it has important consequences, one of which will be explored in the next section. } これは些細な文法規則に見えるかもしれませんが、実は重要な事です。詳しくは次節で述べます。 #co(){To conclude this section about integration with 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 のインターフェイスを実装したりできる、という事も述べておくべきでしょう。 #co(){Other regions such as the french speaking part of Switzerland use the same covnetions.} #hr(width=80%)  *1 例えばスイスでフランス語を話す地域など、他の地方でも同じ記法が使用されます。 #center(){[[前ページ>Tutorial_2]]  [[目次>Tutorial和訳]]  [[次ページ>Tutoarial_4]]} ---- #comment()

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

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