部落客廣告聯播

2007年8月1日 星期三

JAVA FX 基礎

  1. 分兩大部分:
    1. Java FX script
      • 靜態宣告式script語言
    2. Java FX Mobile
      • 用來開發應用於可攜裝置的平台
  2. 4個基本型別:
    1. String (java.lang.String)
    2. Boolean (java.lang.Boolean)
    3. Number (java.lang.Number)
    4. Integer (java.lang.BigInteger,byte, short, int, long)
      宣告方法: var varName:typeName=value;
      var varName:typeName=[itemval1,itemval2,itemval3];
  3. 邏輯運算子
    Java JavaFX
    && and
    || or
    ! not
  4. 在fx檔將變數寫到std out:

    import java.lang.System;
    var i:Integer=10;
    var str:String="String ddd";
    System.out.println("Hello {i} {str}");
  5. 宣告變數其尾(=號前)可指定下列符號,以限定其集合限制:
    1. ? 選擇性,可為null(零或一個元數)
    2. + 一或多個元素
    3. * 零或多個元素
      Ex.        var nums:Number* = [5, 9, 13];

  6. 字串可包於單引號或雙引號間。
  7. 若要串接字串,相較於java使用+號,java fx在字串中使用大括號,如下:

    var name="mark";
    var title="Manager";
    var complete_name="{title} {name}";

  8. 在上面的大括號也可以是判斷式,如下:

    var truth="1>2 is {if 1>2 then "true" else "false" }";

    這種情況下雙引號內有雙引號,是允許的。(相較java不可)
  9. java fx陣列代表一串物件,但陣列本身不是物件。
  10. sizeof varName 可用來取得varName陣列的包含元素數目。
  11. 宣告1到100整數陣列:
    var myint:Integer=[1..100];
  12. 陣列操作語法: insert、delete、select 、foreach
    1. insert
      var ints=[1,2,3];
      insert 20 into ints; //[1,2,3,20]
      insert 30 as first into ints; //[30,1,2,3,20]
      insert [100,200] as last into ints; //[30,1,2,3,20,100,200]
      insert 99 before ints[.==2]; //[30,1,99,2,3,20,100,200]
      insert 88 after ints[5]; //[30,1,99,2,3,88,20,100,200]
    2. delete
      var ints=[1,2,3,4,5];
      delete ints[.==3]; //[1,2,4,5]
      delete ints[.>3]; //[1,2]
      delete ints; //[ ]
    3. select
      var s:Integer*= select n+n from n in [1,2,3]; //[2,4,6]
      var s2:Integer*=select n from n in [1,2,3,4,5,6] where (n%2==0); //[2,4,6]
      var s3:Integer*=select n*m from n in [1,2],m in[3,4] ; //[3,4,6,8]
    4. foreach
      var s:Integer*= foreach(n in [1,2],m in [3,4]) n*m;
  13. 跳脫java fx關鍵字,將關鍵字放在<< 和 >>之間。
  14. class宣告:
    1. 包含有attribute、function、operation
    2. 可extends多個class
    3. 範例:(from Learning JavaFX Script, Part 1: An Introduction to JavaFX Script for Java Programmers)

      class Person {

      attribute name: String;
      attribute parent: Person;
      attribute children: Person*;

      function getFamilyIncome(): Number;
      function getNumberOfChildren(): Number;

      operation marry(spouse: Person): Boolean;
      }

  15. 使用物件:

    1. import java.lang.System;

      class X {
      attribute a: Number;
      attribute b: Number;
      }

      attribute X.a = 10;
      attribute X.b = -1;

      var x = new X();
      var myXClass = X {
      a: 10
      b: -1
      };

    2. import java.util.Date;
      import java.lang.System;

      var date1 = new Date(95, 4, 23); // call a Java constructor
      var date2 = Date { // create the same date as an object literal
      month: 4
      date: 23
      year: 95
      };

      System.out.println(date1 == date2); // prints true
  16. 大不同:java fx的function和operation主體都在class定義之外。

    1. operation Person.marry(spouse: Person): Boolean {
      // Body of operation
      }
    2. class定義中一定要有參數和回傳類型,但在class定義外的function或operation主體可省略。
      operation Person.marry() {
      // Body of operation
      }
  17. java fx沒有constructor和setter,取而代之的是使用Trigger:
    (object creation、insert、delete、replace)
    1. objectCreation
      import java.lang.*;
      class X{ attribute serial:Integer*;}
      trigger on new X{ insert [10,20] as last into serial}
      var x=new X();
    2. insert
      import java.lang.*;
      class X{ attribute serial:Integer*;}
      trigger on insert num into X.serial{ System.out.println("hi"); }
      var x = new X();
      insert 12 into x.nums;
    3. delete
      import java.lang.*;
      class X{ attribute serial:Integer*;}
      trigger on delete num from X.serial{ System.out.println("hi"); }
      var x = new X();
      delete x.serial[1];
    4. replace
      import java.lang.*;
      class X{ attribute serial:Integer*;}
      trigger on x.serial[olderVal]=newVal{ System.out.println("hi"); }
      var x = new X();
      delete x.serial[1];
  18. do{}敘述區塊:
    java fx程式是在AWT Event Dispatch Thread (EDT)中執行,而將程式碼放在do區塊中,可使該段程式於背景thread同步執行。
  19. 增進式評估:使用bind。

沒有留言: