2008年12月14日日曜日

JavaFX Access Modifiers

Default Access
  1. var x;  
  2. var x : String;  
  3. var x = z + 22;  
  4. var x = bind f(q);  

この場合、変数はスクリプト内からのみ 初期化、上書き、参照、代入、束縛ができる。他の source files からは参照やアクセスはできない


The package Access Modifier

変数や関数やクラスを、同じパッケージ内の他のコードからアクセスできるようにするには、package access modifier を使う
  1. package var x;  

この access modifier を package 宣言を混同しないように注意する!

Example:
  1. // Inside file tutorial/one.fx  
  2.   
  3. // places this script in the "tutorial" package  
  4. package tutorial;   
  5.   
  6. // this is the "package" access modifier  
  7. package var message = "Hello from one.fx!";   
  8. package function printMessage() {  
  9.      println("{message} (in function printMessage)");  
  10. }  
  11.   
  12. // Inside file tutorial/two.fx  
  13. package tutorial;  
  14. println(one.message);  
  15. one.printMessage();  

この例を tutorial の上のディレクトリでコンパイルする
  1. javafxc tutorial/one.fx tutorial/two.fx  
  2. javafx tutorial/two  

The output is:
  1. Hello from one.fx!  
  2. Hello from one.fx! (in function printMessage)  



The protected Access Modifier

protected access modifier は変数や関数を、同じパッケージの他のコードや、他のパッケージ内のサブクラスからアクセスできるようにする

Example:
  1. // Inside file tutorial/one.fx  
  2. package tutorial;  
  3. public class one {  
  4.      protected var message = "Hello!";  
  5. }  
  6.   
  7. // Inside file two.fx  
  8. import tutorial.one;  
  9. class two extends one {  
  10.      function printMessage() {  
  11.           println("Class two says {message}");  
  12.      }  
  13. };  
  14.   
  15. var t = two{};  
  16. t.printMessage();  

コンパイルする
  1. javafxc tutorial/one.fx two.fx  
  2. javafx two  

The output is:
  1. Class two says Hello!  

ただし、このprotected access modifier は class に付けることはできない
class one に public がついているのはそのため


The public Access Modifier

public なクラス、変数、関数は、どのパッケージ内のどのクラス、スクリプトからもアクセスできる

Example:
  1. // Inside file tutorial/one.fx  
  2. package tutorial;  
  3. public def someMessage = "This is a public script variable, in one.fx";  
  4. public class one {  
  5.      public var message = "Hello from class one!";  
  6.      public function printMessage() {  
  7.           println("{message} (in function printMessage)");  
  8.      }  
  9. }  
  10.   
  11. // Inside file two.fx  
  12. import tutorial.one;  
  13. println(one.someMessage);  
  14. var o = one{};  
  15. println(o.message);  
  16. o.printMessage();  

コンパイルする
  1. javafxc tutorial/one.fx two.fx  
  2. javafx two  

Output:
  1. This is a public script variable, in one.fx  
  2. Hello from class one!  
  3. 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:
  1. // Inside file tutorial/one.fx  
  2. package tutorial;  
  3. public-read var x = 1;  
  4.   
  5. // Inside tutorial/two.fx  
  6. package tutorial;  
  7. println(one.x);  

コンパイルする
  1.    
  2. javafxc tutorial/one.fx tutorial/two.fx  
  3. javafx tutorial/two  

tutorial/one.fx の x は外部から読めるので、出力は "1" になる

次に、x の値を変更しようとしてみる
  1. // Inside tutorial/two.fx  
  2. package tutorial;  
  3. one.x = 2;   
  4. println(one.x);  

これはコンパイルエラーになる
  1. tutorial/two.fx:3: x has script only (default) write access in tutorial.one  
  2. one.x = 2;  
  3.    ^  
  4. 1 error  

x の値を変更するには、x の write access を広げなければならない
  1. // Inside file tutorial/one.fx  
  2. package tutorial;  
  3. package public-read var x = 1;  
  4.   
  5. // Inside tutorial/two.fx  
  6. package tutorial;  
  7. one.x = 2;  
  8. 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:
  1. // Inside file tutorial/one.fx  
  2. package tutorial;  
  3. public class one {  
  4.      public-init var message;  
  5. }  
  6.   
  7. // Inside file two.fx  
  8. import tutorial.one;  
  9. var o = one {  
  10.      message: "Initialized this variable from a different package!"  
  11. }  
  12. println(o.message);  

コンパイルする
  1. javafxc tutorial/one.fx two.fx  
  2. javafx two  

Output :
  1. Initialized this variable from a different package!  

異なるパッケージ内の object literal が message variable を初期化できる
しかし、次の write access は script-only であるので、値を変えられない
よって次のコードはコンパイルエラーになる
  1. // Inside file two.fx  
  2. import tutorial.one;  
  3. var o = one {  
  4.      message: "Initialized this variable from a different package!"  
  5. }  
  6. o.message = "Changing the message..."// WON'T COMPILE  
  7. println(o.message);  

コンパイルエラーになる
  1. two.fx:12: message has script only (default) write access in tutorial.one  
  2. o.message = "Changing the message..."// WON'T COMPILE  
  3.  ^  
  4. 1 error  

0 件のコメント:

コメントを投稿