JavaFX#3 การเขียน JavaFX Script

posted on 08 Jun 2009 17:26 by interviewz  in JavaFX

 

 

การประกาศตัวแปร (Declaring Script Variables)

 

การประกาศตัวแปร  คีย์เวิร์ดที่ใช้คือ var หรือ def

ความแตกต่างคือ  var เมื่อประกาศแล้วสามารถกำหนดค่าให้มันได้ตลอดช่วงชีวิตของ script  

ส่วน def เป็นเหมือนค่าคงที่ จะถูกกำหนดค่าในครั้งแรก

ตัวอย่าง เรากำหนดค่าให้ทั้ง numOne และ numTwo แต่ยังไม่กำหนดให้ result เพราะมันจะได้มาจากการคำนวณ

 

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}");

}

 

สังเกตได้ว่าเราไม่ต้องระบุ ตอนสร้างตัวแปรเหล่านี้ ว่าเป็นข้อมูลตัวเลข หรือตัวอักษร

เพราะ compiler ฉลาดพอที่จะเลือกให้ตรงตามจุดประสงค์ของเรา ว่าเราใช้ตัวแปรนั้นทำอะไร

ลักษณะแบบนี้เรียกว่า type inference เป็นการสรุปประเภทให้เรา ซึ่งทำให้งานของนักพัฒนาง่ายขึ้นอีกนิด เพราะมันทำให้เราอิสระจากการประกาศประเภทของตัวแปร

 

 

 

 

การกำหนดและเรียกฟังก์ชั่น (Defining and Invoking Script Functions)

 

Function คือ บล็อกของโค๊ดที่ทำงานตามที่กำหนด โค๊ดสีแดงข้างล่างนี้แสดงถึงฟังก์ชั่น 4 ฟังก์ชั่น

แต่ละอันจะทำการคำนวณและแสดงผล

 

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}");

}

 

อย่าลืมว่า โค๊ดสีแดงด้านบนนั้นจะยังไม่ทำงานจนกว่าจะมีการเรียกใช้ (invoke) ซึ่งแสดงด้วยสีเขียวด้านล่าง

และมันไม่สำคัญว่าจะต้องประกาศฟังก์ชั่นก่อนเรียกใช้ หรือเรียกใช้ก่อนประกาศฟังก์ชั่น

 

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}");

}

 

เงื่อนไขในการประกาศฟังก์ชั่นคือ ชื่อฟังก์ชั่นต้องขึ้นต้นด้วยอักษรตัวเล็ก และคำต่อไปในชื่อให้อักษรตัวแรกเป็นตัวอักษรใหญ่

เช่น เราจะตั้งชื่อว่า stop current songให้เขียนเป็น stopCurrentSong

 

 

 

 

การส่งผ่านค่าตัวแปรไปยังฟังก์ชั่น (Passing Arguments to Script Functions)

 

ทีนี้เราลองไม่ใช้ตัวแปร numOne และ numTwo แต่ใช้การส่งค่าไปยังฟังก์ชั่น

 

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}");

}

 

 

ผลลัพธ์ของ script เป็นดังนี้

 

100 + 10 = 110

50 - 5 = 45

25 * 4 = 100

500 / 2 = 250

 

 

 

 

 

การคืนค่าจากฟังก์ชั่น (Returning Values from Script Function)

 

เราสามารถเปลี่ยนฟังก์ชั่น add ให้มีการคืนค่าได้

 

function add(argOne: Integer, argTwo: Integer) : Integer {

     result = argOne + argTwo;

     println("{argOne} + {argTwo} = {result}");

     return result;

 

}

 

โค๊ดสีแดงอันแรก แสดงให้เห็นว่ามีการคืนค่าเป็นประเภท integer

อันที่สอง เป็นบรรทัดที่เกิดการคืนค่าขึ้นจริงๆ

 

ตอนนี้เราสามารถเรียกฟังก์ชั่น add แบบนี้ได้

 

var total;

 

total = add(1,300) + add(23,52);

 

 

ถ้าไม่มีการระบุการคืนค่า โดย default แล้วจะเป็น void 

 

 

 

 

การรับค่าตัวแปรจาก command-line

(Accessing Command-Line Arguments)

 

สุดท้ายนี้ script สามารถรับ command-line argument ได้ ตัวอย่างนี้จะรับเลขจากผู้ใช้ เพื่อนำมาคำนวณ

 

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}");

}

 

 ฟังก์ชั่น run เป็นฟังก์ชั่นพิเศษ จะเก็บ command-line ลงใน args ซึ่งเป็น sequence of String object (เหมือนอาเรย์ของ String)

ในการรัน script นี้ ผู้ใช้จะต้องกรอกข้อมูลตัวเลขที่หนึ่งและสอง ในตอน runtime

 

javafx calculator 100 50

 

ผลลัพธ์คือ

 

100 + 50 = 150

100 - 50 = 50

100 * 50 = 5000

100 / 50 = 2

 
สังเกตว่าเวอร์ชั่นก่อนหน้านี้ของ calculator script นี้เราไม่ได้เขียนฟังก์ชั่น run ไว้ เราแค่เขียนโค๊ดต่างๆและมันก็รันไปตามที่เราคาดหวังไว้ ในกรณีนี้ compiler จะสร้าง ฟังก์ชั่น run ที่ไม่มี args มาให้เราอัตโนมัติ แต่เมื่อเราระบุฟังก์ชั่น run ของเราเอง ชื่อของตัวแปร args เราสามารถตั้งเองว่าอะไรก็ได้ เช่น arg , ARDS , _ARGS_ , argv เป็นต้น

 

และในเวอร์ชั่นนี้เรานำตัวแปร numOne และ numTwo กลับมาใช้ในฟังก์ชั่น run (แบบนี้เรียก local variable) เพื่อนำมาใช้แปลงค่า String ที่รับจาก command-line ให้เป็น integer

 

// Convert Strings to Integers

def numOne = java.lang.Integer.parseInt(args[0]);

def numTwo = java.lang.Integer.parseInt(args[1]);

 

 

 
 
CREDIT : ภาคภาษาอังกฤษดั้งเดิม http://java.sun.com
ไปดูตัวอย่างอื่นเพิ่มเติมได้ที่ http://java.sun.com/javafx/1/tutorials/core/writingScripts/
 
 

 

 

Comment



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

น่าสนใจนะนี่บล็อคนี่ confused smile

Hot! Hot!

บทความเก่าๆ ก็เขียนดีครับ

#1 By TonHor on 2009-06-09 16:27


แวะมารดน้ำอะ...

เราจะรอดูดอกไม้เต็มต้นไม้นะ..

^^

#2 By OaT (124.120.179.224) on 2009-06-10 00:28

โห คุณวีวิกานี่เก่งจริงๆเลยเนอะ ทำบ้านได้น่าสนใจมากเลย
ว่าแต่ต้นไม้ดอกไม่เห็นจาขึ้นเลยละ นั่งรดน้ำทั้งวันแล้วเนี่ย อิอิ

#3 By cakeclub (125.24.207.108) on 2009-06-12 03:20

ตอนนี้กำลังสนใจทำโปรเจคจบเกี่ยวกับ javafx อยู่ครับ
แต่มีปัญหาที่ว่า javafx สามารถสร้างเกมออนไลน์ที่คล้ายๆเกม restaurant city บน facebook หรือเกม harvest moon ได้หรือเปล่า

พอจะรู้บ้างมั้ยครับbig smile

#4 By hellolala (161.246.1.38) on 2009-06-17 18:22