学习JavaFX脚本语言----10,11(完)
Lesson 10: Packages
目录
- Step 1: 选择一个包名
- Step 2: 创建目录
- Step 3: 添加包声明
- Step 4: 添加访问权限
- Step 5: 编译源码
- Step 6: 使用类
到这里,你对javaFX的基础应该比较熟悉了。但是对于源文件的存放,你可能还不是很清楚(你现在可能是用的一个文件夹来存放所有的例子代码)我们将把代码放到包中,来改变你对存放代码的认识。(We can improve our overall organization by placing our code into packages.)
包能够让你按功能来将代码分类保存。它还给你的类一个唯一的命名空间。我们将在下面一步步的来将Address类存放到一个特殊的包内。
-Step 1: 选择一个包名
在我们修改代码前,我们需要给包起个名字。由于我们的Address类是假设用在addressbook应用上的,所以我们使用"addressbook"作为包名。
-Step 2: 创建目录
接着,我们必须创建一个addressbook目录。这个目录里面将包含所有我们设计的属于addressbook这个包的.fx文件。你可以在任意的地方创建目录。我们在例子里面使用
/home/demo/addressbook,但是脚本必须在一个和包名相同的目录里面,这里就是
addressbook
-Step 3: 添加包声明
现在,到addressbook目录里面创建Address.fx源代码文件。粘贴下面的代码到源代码文件里面去。第一行提供了一个包声明,这将表示这个类属于addressbook这个包。
package addressbook;class Address { var street: String; var city: String; var state: String; var zip: String;}package addressbook;public class Address { public var street: String; public var city: String; public var state: String; public var zip: String;}// Approach #1addressbook.Address { street: "1 Main Street"; city: "Santa Clara"; state: "CA"; zip: "95050";}// Approach #2import addressbook.Address;Address { street: "1 Main Street"; city: "Santa Clara"; state: "CA"; zip: "95050";}var x;var x : String;var x = z + 22;var x = bind f(q);
package var x;
// Inside file tutorial/one.fxpackage tutorial; // places this script in the "tutorial" packagepackage var message = "Hello from one.fx!"; // this is the "package" access modifierpackage function printMessage() { println("{message} (in function printMessage)");}// Inside file tutorial/two.fxpackage tutorial;println(one.message);one.printMessage();// Inside file tutorial/one.fxpackage tutorial;public class one { protected var message = "Hello!";}// Inside file two.fximport tutorial.one;class two extends one { function printMessage() { println("Class two says {message}"); }};var t = two{};t.printMessage();// Inside file tutorial/one.fxpackage 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.fximport tutorial.one;println(one.someMessage);var o = one{};println(o.message);o.printMessage();// Inside file tutorial/one.fxpackage tutorial;public-read var x = 1;// Inside tutorial/two.fxpackage tutorial;println(one.x);
// Inside tutorial/two.fxpackage tutorial;one.x = 2; println(one.x);
// Inside file tutorial/one.fxpackage tutorial;package public-read var x = 1;// Inside tutorial/two.fxpackage tutorial;one.x = 2;println(one.x);
// Inside file tutorial/one.fxpackage tutorial;public class one { public-init var message;}// Inside file two.fximport tutorial.one;var o = one { message: "Initialized this variable from a different package!"}println(o.message);// Inside file two.fximport tutorial.one;var o = one { message: "Initialized this variable from a different package!"}o.message = "Changing the message..."; // WON'T COMPILEprintln(o.message);
8 楼 gml520 2008-12-14 rubyeye 写道