「Tutorial_2」の編集履歴(バックアップ)一覧はこちら
Tutorial_2 - (2010/10/21 (木) 13:15:00) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
[[トップ>Top]] > [[Tutorial>Tutorial和訳]] > [[2 A first example>Tutorial_2]]
#co(){*2 A first example
a first example, we will use the standard Hello world program. It is not very fascinating but makes it easy to demonstrate the use of the Scala tools without knowing too much about the language. Here is how it looks: }
*2 最初の例
最初の例として、標準的な &italic(){Hello world} プログラムを用います。これはあまり興味深いとは言えませんが、Scala 言語に関する知識をあまり必要とせずに、Scala ツールの使い方を簡単に示すことができます。このようになります。
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
#co(){The structure of this program should be familiar to Java programmers: it consists of one method called main which takes the command line arguments, an array of strings, as parameter; the body of this method consists of a single call to the predefined method println with the friendly greeting as argument. The main method does not return a value (it is a procedure method). Therefore, it is not necessary to declare a return type. }
Java プログラマにはプログラムの構造はお馴染みのものでしょう。main と呼ばれる1つのメソッドがあり、main はパラメータとしてコマンドライン引数を文字列配列として受け取ります。メソッドの本体では定義済みメソッドである println を、友好的な挨拶文を引数として1回呼び出しています。main メソッドは値を返しません(手続きメソッドです)。それゆえ戻り値型を宣言する必要はありません。
#co(){What is less familiar to Java programmers is the object declaration containing the main method. Such a declaration introduces what is commonly known as a singleton object, that is a class with a single instance. The declaration above thus declares both a class called HelloWorld and an instance of that class, also called HelloWorld. This instance is created on demand, the first time it is used. }
Java プログラマにとって馴染みが薄いのは、main メソッドを囲んでいる &bold(){object} という宣言でしょう。このような宣言によって、&italic(){シングルトンオブジェクト}として知られるただ一つのインスタンスしか持たないクラスを導入できます。この宣言はすなわち、HelloWorld と呼ばれるクラスと、そのクラスの同じく HelloWorld と呼ばれるインスタンスの両方をいっぺんに宣言します。インスタンスは必要に応じて、つまり最初に使用される時に、生成されます。
#co(){The astute reader might have noticed that the main method is not declared as static here. This is because static members (methods or fields) do not exist in Scala. Rather than defining static members, the Scala programmer declares these members in singleton objects. }
賢明なる読者のみなさんは既にお気づきかもしれませんが、ここでは main メソッドは static として宣言されていません。というのは、静的メンバ(メソッドやフィールド)は Scala には存在しないからです。Scala プログラミングでは、静的メンバを定義するのではなくて、シングルトンオブジェクトのメンバを宣言します。
#co(){**2.1 Compiling the example
To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.}
#co(){If we save the above program in a file called HelloWorld.scala, we can compile it by issuing the following command (the greater-than sign ‘>’ represents the shell prompt and should not be typed): }
**2.1 例をコンパイルする
この例をコンパイルするには、Scala コンパイラの scalac を使用します。scalac は多くのコンパイラと同じように働きます。引数としてソースファイル1つと、もしかしたらオプションをいくつか取り、オブジェクトファイルを1つあるいはいくつか生成します。生成されるオブジェクトファイルは標準的な Java のクラスファイルです。上記プログラムを HelloWorld.scala というファイルに保存すれば、下記のコマンドでコンパイルできます。(不等号 '>' はシェルプロンプトなので、入力しないで下さい。)
> scalac HelloWorld.scala
#co(){This will generate a few class files in the current directory. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command, as the following section shows. }
これによってクラスファイルがいくつかカレントディレクトリに生成されます。その一つは HelloWorld.class と呼ばれ、scala コマンドによって直接実行可能なクラスを含んでいますが、それは次節で示します。
#co(){**2.2 Running the example
Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output: }
**2.2 例を走らせる
Scala プログラムをコンパイルしたあとは、scala コマンドにて実行できます。使い方は Java プログラムの実行で使う java コマンドと非常によく似ており、同じオプションが使えます。上記の例は下記のコマンドで実行でき、期待通りに出力されます。
> scala -classpath . HelloWorld
Hello, world!
#center(){[[前ページ>>Tutorial_1]] [[目次>>Tutorial和訳]] [[次ページ>>Tutorial_3]]}
----
- 「2.2 例を走らせる」は、「2.2 例を実行する」でもよさそうです。 -- satofumi (2009-06-29 03:09:41)
- 誤字「引数として1つのソースファイスと、」→「引数として1つのソースファイルと、」 -- kohyama (2009-12-05 03:26:33)
#comment()
[[トップ>Top]] > [[Tutorial>Tutorial和訳]] > [[2 A first example>Tutorial_2]]
#co(){*2 A first example
a first example, we will use the standard Hello world program. It is not very fascinating but makes it easy to demonstrate the use of the Scala tools without knowing too much about the language. Here is how it looks: }
*2 最初の例
最初の例として、標準的な &italic(){Hello world} プログラムを用います。これはあまり興味深いとは言えませんが、Scala 言語に関する知識をあまり必要とせずに、Scala ツールの使い方を簡単に示すことができます。このようになります。
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
#co(){The structure of this program should be familiar to Java programmers: it consists of one method called main which takes the command line arguments, an array of strings, as parameter; the body of this method consists of a single call to the predefined method println with the friendly greeting as argument. The main method does not return a value (it is a procedure method). Therefore, it is not necessary to declare a return type. }
Java プログラマにはプログラムの構造はお馴染みのものでしょう。main と呼ばれる1つのメソッドがあり、main はパラメータとしてコマンドライン引数を文字列配列として受け取ります。メソッドの本体では定義済みメソッドである println を、友好的な挨拶文を引数として1回呼び出しています。main メソッドは値を返しません(手続きメソッドです)。それゆえ戻り値型を宣言する必要はありません。
#co(){What is less familiar to Java programmers is the object declaration containing the main method. Such a declaration introduces what is commonly known as a singleton object, that is a class with a single instance. The declaration above thus declares both a class called HelloWorld and an instance of that class, also called HelloWorld. This instance is created on demand, the first time it is used. }
Java プログラマにとって馴染みが薄いのは、main メソッドを囲んでいる &bold(){object} という宣言でしょう。このような宣言によって、&italic(){シングルトンオブジェクト}として知られるただ一つのインスタンスしか持たないクラスを導入できます。この宣言はすなわち、HelloWorld と呼ばれるクラスと、そのクラスの同じく HelloWorld と呼ばれるインスタンスの両方をいっぺんに宣言します。インスタンスは必要に応じて、つまり最初に使用される時に、生成されます。
#co(){The astute reader might have noticed that the main method is not declared as static here. This is because static members (methods or fields) do not exist in Scala. Rather than defining static members, the Scala programmer declares these members in singleton objects. }
賢明なる読者のみなさんは既にお気づきかもしれませんが、ここでは main メソッドは static として宣言されていません。というのは、静的メンバ(メソッドやフィールド)は Scala には存在しないからです。Scala プログラミングでは、静的メンバを定義するのではなくて、シングルトンオブジェクトのメンバを宣言します。
#co(){**2.1 Compiling the example
To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.}
#co(){If we save the above program in a file called HelloWorld.scala, we can compile it by issuing the following command (the greater-than sign ‘>’ represents the shell prompt and should not be typed): }
**2.1 例をコンパイルする
この例をコンパイルするには、Scala コンパイラの scalac を使用します。scalac は多くのコンパイラと同じように働きます。引数としてソースファイル1つと、もしかしたらオプションをいくつか取り、オブジェクトファイルを1つあるいはいくつか生成します。生成されるオブジェクトファイルは標準的な Java のクラスファイルです。上記プログラムを HelloWorld.scala というファイルに保存すれば、下記のコマンドでコンパイルできます。(不等号 '>' はシェルプロンプトなので、入力しないで下さい。)
> scalac HelloWorld.scala
#co(){This will generate a few class files in the current directory. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command, as the following section shows. }
これによってクラスファイルがいくつかカレントディレクトリに生成されます。その一つは HelloWorld.class と呼ばれ、scala コマンドによって直接実行可能なクラスを含んでいますが、それは次節で示します。
#co(){**2.2 Running the example
Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output: }
**2.2 例を走らせる
Scala プログラムをコンパイルしたあとは、scala コマンドにて実行できます。使い方は Java プログラムの実行で使う java コマンドと非常によく似ており、同じオプションが使えます。上記の例は下記のコマンドで実行でき、期待通りに出力されます。
> scala -classpath . HelloWorld
Hello, world!
#center(){[[前ページ>Tutorial_1]] [[目次>Tutorial和訳]] [[次ページ>Tutorial_3]]}
----
- 「2.2 例を走らせる」は、「2.2 例を実行する」でもよさそうです。 -- satofumi (2009-06-29 03:09:41)
- 誤字「引数として1つのソースファイスと、」→「引数として1つのソースファイルと、」 -- kohyama (2009-12-05 03:26:33)
#comment()