- var x;
- var x : String;
- var x = z + 22;
- var x = bind f(q);
この場合、変数はスクリプト内からのみ 初期化、上書き、参照、代入、束縛ができる。他の source files からは参照やアクセスはできない
The package Access Modifier
変数や関数やクラスを、同じパッケージ内の他のコードからアクセスできるようにするには、package access modifier を使う
- package var x;
この access modifier を package 宣言を混同しないように注意する!
Example:
- // Inside file tutorial/one.fx
- // places this script in the "tutorial" package
- package tutorial;
- // this is the "package" access modifier
- package var message = "Hello from one.fx!";
- package function printMessage() {
- println("{message} (in function printMessage)");
- }
- // Inside file tutorial/two.fx
- package tutorial;
- println(one.message);
- one.printMessage();
この例を tutorial の上のディレクトリでコンパイルする
- javafxc tutorial/one.fx tutorial/two.fx
- javafx tutorial/two
The output is:
- Hello from one.fx!
- Hello from one.fx! (in function printMessage)
The protected Access Modifier
protected access modifier は変数や関数を、同じパッケージの他のコードや、他のパッケージ内のサブクラスからアクセスできるようにする
Example:
- // Inside file tutorial/one.fx
- package tutorial;
- public class one {
- protected var message = "Hello!";
- }
- // Inside file two.fx
- import tutorial.one;
- class two extends one {
- function printMessage() {
- println("Class two says {message}");
- }
- };
- var t = two{};
- t.printMessage();
コンパイルする
- javafxc tutorial/one.fx two.fx
- javafx two
The output is:
- Class two says Hello!
ただし、このprotected access modifier は class に付けることはできない
class one に public がついているのはそのため
The public Access Modifier
public なクラス、変数、関数は、どのパッケージ内のどのクラス、スクリプトからもアクセスできる
Example:
- // Inside file tutorial/one.fx
- package tutorial;
- public def someMessage = "This is a public script variable, in one.fx";
- public class one {
- public var message = "Hello from class one!";
- public function printMessage() {
- println("{message} (in function printMessage)");
- }
- }
- // Inside file two.fx
- import tutorial.one;
- println(one.someMessage);
- var o = one{};
- println(o.message);
- o.printMessage();
コンパイルする
- javafxc tutorial/one.fx two.fx
- javafx two
Output:
- This is a public script variable, in one.fx
- Hello from class one!
- Hello from class one! (in function printMessage)
The public-read Access Modifier
public-read access modifier は 変数を次のように定義する
変数は public のように参照することができるが、同じスクリプト内からのみ書き込める
この書き込み権限を広げるには、package or protected modifier を付ける
例)package public-read, protected public-read
こうすると、write access に package or protected level が設定される
Example:
- // Inside file tutorial/one.fx
- package tutorial;
- public-read var x = 1;
- // Inside tutorial/two.fx
- package tutorial;
- println(one.x);
コンパイルする
- javafxc tutorial/one.fx tutorial/two.fx
- javafx tutorial/two
tutorial/one.fx の x は外部から読めるので、出力は "1" になる
次に、x の値を変更しようとしてみる
- // Inside tutorial/two.fx
- package tutorial;
- one.x = 2;
- println(one.x);
これはコンパイルエラーになる
- tutorial/two.fx:3: x has script only (default) write access in tutorial.one
- one.x = 2;
- ^
- 1 error
x の値を変更するには、x の write access を広げなければならない
- // Inside file tutorial/one.fx
- package tutorial;
- package public-read var x = 1;
- // Inside tutorial/two.fx
- package tutorial;
- one.x = 2;
- println(one.x);
これをコンパイルして走らせると、出力は "2" になる
The public-init Access Modifier
public-init access modifier は変数を次のように定義する
変数は どのパッケージの object literals によって public に初期化できる
しかし、次の write access は public-read と同じマナーでコントロールされる
(default は script-level の write access だが、package か protected で access を広げることができる)
この変数の値はどのパッケージからでも読める
Example:
- // Inside file tutorial/one.fx
- package tutorial;
- public class one {
- public-init var message;
- }
- // Inside file two.fx
- import tutorial.one;
- var o = one {
- message: "Initialized this variable from a different package!"
- }
- println(o.message);
コンパイルする
- javafxc tutorial/one.fx two.fx
- javafx two
Output :
- Initialized this variable from a different package!
異なるパッケージ内の object literal が message variable を初期化できる
しかし、次の write access は script-only であるので、値を変えられない
よって次のコードはコンパイルエラーになる
- // Inside file two.fx
- import tutorial.one;
- var o = one {
- message: "Initialized this variable from a different package!"
- }
- o.message = "Changing the message..."; // WON'T COMPILE
- println(o.message);
コンパイルエラーになる
- two.fx:12: message has script only (default) write access in tutorial.one
- o.message = "Changing the message..."; // WON'T COMPILE
- ^
- 1 error
0 件のコメント:
コメントを投稿