2009-05-01

JavaFX Script Learning: Basic Knowledge

Declaring Variables:
There are two type variables in JavaFX, var or def, we can use 'var' and 'def' keyword to declare varibles. the difference between the two is that var may be assigned new value while def not, so def is a little like final variables in Java.
In JavaFX we can do not explicitly specify variables as its date type, compiler can figure out what is the data types you need, this called type inference.

Defining and Invoking Functions:
a basic format of function define:
function add() { }
a function with arguments:
function add(argOne: Integer, argTwo: Integer) {}
a function with returnvalue of Integer:
function add() : Integer { var resule = 11; return resule;}
Note that if no return value is specified, a function returns Void by default.
Accessing Command-Line Arguments:
function run(args : String[]) {}
Note that run function is special that serves as the script's main entry point.
Invoke a function just like invoke a method in Java.

Using Object:
def myAddress = Address { street: "I don't know"; }
this line code create a Object of Address and assign the value of street instance variable of Address.

Data Types:
In JavaFX there are 6 build-in data types: String, Number and Integer, Boolean, Duration, Void, Null.
String can be declared by single or double quotes.
Number is floating-point number while Integer is integer number.
Duriation type represents a fixed unit of time, it should be notated with time literals for example:
5ms //5 milliseconds
5s //5 seconds
5m //5 minutes
5h //5 hour


Void is used toe indicate that a function does not return any value.
null i a special value used to indicate a missing normal value, null is not the same as zero or empty string. you can use 'null' keyword to comparing if its null, in a word, its like null in java.


Sequences in JavaFX:
Sequences should be declared in square brackets[] ,such as


def weekDays: String[] = ["mon","tue","wed","thu","fri"];
def days : [weekDays, ["sat","sun"]];
def nums= [1..10];
//this declared a sequence contains numbers from 1 to 10 with step 1
def numg= [10..1 step -1];
//this declared a sequence contains number from 10 to 1 with step -1
//Note that if create a descending range the second value should less
// than first
def nums2 = nums[ n | n &;lt 5]
//this will create a sequence with the items from mums where value
//less than 5
def size =sizeof numg;
// this will get the size of numg
def weekend = days[5..6];
//this will create a sequence with the items of days 6 to 7
def weekend2 = days[5..];
//this will create a sequence with the items of days 6 to the end
//of days
def weekdays2 = days[0..&;gt5];
//this will create a sequence with the items of days 1 to 5
def days2 = days[0..&;gt];
//this will create a sequence with all items from days,
//but the last item of days will be excluded


Operators in JavaFX:
+-*/is same to Java
mod get remainder
not is inverts the balue of boolean
and or is same to Java
instanceof is same to Java


Expressions in JavaFX:
A block expressions surrounded by curly braces and must be sparated by semicolons. The VALUE of block expression is the value of the last expression. var and def are expressions.
Look for expression:
var nums= [1..10] ;
for(num in nums) { println(nums); }

没有评论: