第二課 編寫代碼
一、聲明變量
認識兩個關鍵字:def 和 var。
兩者區別是 def 定義常量,var 定義變量。
代碼示例1:
def numOne = 100;
def numTwo = 2;
var result;
add();
subtract();
multiply();
divide();
function add() {
result = numOne + numTwo;
println("{numOne} + {numTwo} = {result}");
}
function subtract() {
result = numOne - numTwo;
println("{numOne} - {numTwo} = {result}");
}
function multiply() {
result = numOne * numTwo;
println("{numOne} * {numTwo} = {result}");
}
function divide() {
result = numOne / numTwo;
println("{numOne} / {numTwo} = {result}");
}
[說明]:以上的代碼中變量沒有類型,因爲編譯器可以識別出正確的類型。
示例代碼2:
def width = (6 * (82 + 10)) + 20;
def canvasWidth = width -10;
def canvasHeight = 275;
var fgUrl = "{__DIR__}images/flower.jsp";
var bgUrl = "{__DIR__}images/water.jsp";
[說明]:變量定義中可以是一個表達式。
二、定義和調用函數
定義函數的格式
function 函數名(參數1,...):類型{
語句;
...
}
示例代碼3:
def numOne = 100; def numTwo = 2; var result; add(); subtract(); multiply(); divide(); function add() { result = numOne + numTwo; println("{numOne} + {numTwo} = {result}"); } function subtract() { result = numOne - numTwo; println("{numOne} - {numTwo} = {result}"); } function multiply() { result = numOne * numTwo; println("{numOne} * {numTwo} = {result}"); } function divide() { result = numOne / numTwo; println("{numOne} / {numTwo} = {result}"); }
[說明]:紅色代碼是函數定義,綠色代碼是函數調用。函數必須調用才能運行,函數可以在其定義的前後位置運行,調用的格式是:函數名();,
示例代碼4:
function stopCurrentSong():Void{
mediaPlayer.stop();
mediaPlayer.media=null;
if(playlist.currentPlayingSong != null) {
playlist.currentPlayingSong.closeMedia();
}
}
funcition playCurentSong():Void {
playlist.currentPlaySong = playlist.songs[playlist.currentSong];
mediaPlayer.media = playlist.currentPlayingSong.getMedia();
mediaPlayer.play();
}
[說明]:黃色代碼行定義的函數帶返回類型
給函數傳遞參數
示例代碼5:
var result; add(100,10); subtract(50,5); multiply(25,4); divide(500,2); function add(argOne: Integer, argTwo: Integer) { result = argOne + argTwo; println("{argOne} + {argTwo} = {result}"); } function subtract(argOne: Integer, argTwo: Integer) { result = argOne - argTwo; println("{argOne} - {argTwo} = {result}"); } function multiply(argOne: Integer, argTwo: Integer) { result = argOne * argTwo; println("{argOne} * {argTwo} = {result}"); } function divide(argOne: Integer, argTwo: Integer) { result = argOne / argTwo; println("{argOne} / {argTwo} = {result}"); }
輸出:
100 + 10 = 110 50 - 5 = 45 25 * 4 = 100 500 / 2 = 250
[說明]:紅色代碼分別是函數的實參和虛參。
帶返回值函數
示例代碼6:
function add(argOne: Integer, argTwo: Integer) : Integer { result = argOne + argTwo; println("{argOne} + {argTwo} = {result}"); return result; }
var total; total = add(1,300) + add(23,52);
[說明]:紅色代碼表示返回值類型和返回代碼語句。
接受命令行參數
JavaFX script可以接受命令行參數
示例代碼7:
var result;
function run(args : String[]) {
// Convert Strings to Integers
def numOne = java.lang.Integer.parseInt(args[0]);
def numTwo = java.lang.Integer.parseInt(args[1]);
// Invoke Functions
add(numOne,numTwo);
subtract(numOne,numTwo);
multiply(numOne,numTwo);
divide(numOne,numTwo);
}
function add(argOne: Integer, argTwo: Integer) {
result = argOne + argTwo;
println("{argOne} + {argTwo} = {result}");
}
function subtract(argOne: Integer, argTwo: Integer) {
result = argOne - argTwo;
println("{argOne} - {argTwo} = {result}");
}
function multiply(argOne: Integer, argTwo: Integer) {
result = argOne * argTwo;
println("{argOne} * {argTwo} = {result}");
}
function divide(argOne: Integer, argTwo: Integer) {
result = argOne / argTwo;
println("{argOne} / {argTwo} = {result}");
}
運行:
javafx calculator 100 50
輸出:
100 + 50 = 150 100 - 50 = 50 100 * 50 = 5000 100 / 50 = 2
[說明]:紅色的代碼是一個run函數,它實際是程序的主入口,不寫這個run函數時,程序也會產生一個虛擬的。在這個函數裏可以使用命令行參數。命令行參數是一個數組,意味着參數可以有多個。參數名args,可以任意取名,比如:"arg
", "ARGS
", "__ARGS__
", "argv
" 。命令行參數類型是String,如果希望它作爲其它類型使用,需要進行轉換,比如上面代碼中的藍色語句。