第二课 编写代码
一、声明变量
认识两个关键字: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,如果希望它作为其它类型使用,需要进行转换,比如上面代码中的蓝色语句。