機械翻訳について

変数の定義

Groovyは動的言語であるため、スクリプト内の変数は、次のようにdefキーワードを使用して動的に入力できます:

// Assign the number 10 to a variable named "counter"
def counter = 10

// Assign the string "Hello" to a variable named "salutation"
def salutation = 'Hello'

// Assign the current date and time to a variable named "currentTime"
def currentTime = now()

defキーワードを使用すると、前述の3つの例のみでなく、任意のタイプの値を格納する適切な型のローカル変数を定義できます。 または、変数に対して特定の型を宣言して、コード内の意図をより明確にすることができます。 たとえば、かわりに次のように記述できます:

// Assign the number 10 to a variable of type Integer named "counter"
Integer counter = 10

// Assign the string "Hello" to a variable named "salutation"
String salutation = 'Hello'

// Assign the current date and time to a variable named "currentTime"
Date currentTime = now()
ノート: 通常、defキーワードを使用するか、独自のプリファレンスに従って変数に特定のタイプを使用するかを選択できますが、変数がビジネス・オブジェクトを保持する必要がある場合は、defキーワードを使用して変数のタイプを定義する必要があります。 詳細は、次の「文字列での置換式の使用」のヒントを参照してください。