Flex入门--Flex基本语法

1 Flex 变量

          var v1:int = 9//int v1 = 9;
          var v2:uint = 10;
          var v3:Boolean = true;
          var v4:Number = 4.5;
          var v5:String = "HelloWorld";
          var v6:String = 'HelloWorld Again';
          var v7:* = 3;  //*类型:任意数据类型
          trace(v7);
          v7 = 'hello';
          trace(v7);
          var v8;      //v8只定义没赋值,为undefined
          trace("v8=",v8);
          var v9:String;
          trace(v9);  //String 类似java中的null
          var v10:Array = [2, 3, 4, 5]; //数组
          trace(v10);
          var v11:Object = {id:3, name:"zhangsan",age:18};                                                                                                                                                                                                                   //map v11是个对象类型,写了个大括号,里面是 属性-值 格式,类似json,或者叫关联性数组。
          trace(v11.name);
Flex中定义变量的格式为: var name:type [ = initvalue]

2 Flex中语句

var v1:Array = ["a", "b", "c", "d"];
for(var i:String in v1{
    //trace(i);  // 0 1 2 3
    trace(v1[i]); // a b c d
}

for each (var propertyValue:String in v1{
    trace(propertyValue);// a b c d
}

var v2:Object = {id:1, name:'lisi', age:19};
for(var i in v2{
    trace(i);        //name id age
    trace(v2[i]);    //lisi 1 19
}

for each (var propertyValue in v2{
    trace(propertyValue);     //lisi 1 19 
}

trace(v2["name"]);            //lisi

语句中的for var i map中的key-valuekey,而arr[i]指的是value

  for each是取出value

3 函数

    public function AS_0400_Functions()

{
    //声明一个匿名函数类型变量traceParameter,定义在后面
    var traceParameter:Function = function   (aParam:String:void//void f(String aParam)
    {
        trace(aParam);
    };
    traceParameter("hello"); // hello
    
    
    var traceArray:Array = new Array(); //定义一个数组
    //数组的0号位置存储了一个函数变量,匿名函数变量,带一个String类型参数
    traceArray[0] = function (aParam:String):void 
    {
        trace(aParam);
    };
    traceArray[0]("hello");
    
    f();
    f('lisi');
    
    f2(1, 3, 5, 4, 6, 8);
    
    var arr : Array = ["hello world!","syx",19];
    say(arr,"test",[1,2,2]);
}
//函数 可以有默认参数值
public function f(name:String = 'zhangsan': void {
    trace(name);
}

//函数可以有变参
//args 是一个存储Object类型的数组
public function f2(...args): void {
    
    trace(args.length);
    for each(var value:String in args{
        trace(value);
    }
}

public function say(...args): void {
    var strTemp : String = "";
    for each(var value : String in args{
        strTemp += value + ":";
    }
    trace(strTemp);
}

4 Flex中面向对象思想

入口:      

      var s:Student = new Student();//定义对象
trace(s.name);//zhangsan
var t:Teacher = new Teacher();
t.f = function() {//给t增加一个函数
trace("f");
}
t.f();// f
delete t.f;
var f:IFlyable = new T();
t.f(); // TypeError: Error #1006: f 不是函数。



   类:

package com.syx.flex.test{
public class Student{
private var _name:String = 'zhangsan';
public function Student() {}
public function set name(name:String) : void { //setName
this._name = name;
}

public function get name():String {
return this._name;
}
}

接口:

package com.syx.flex.test{
public interface IFlyable {
function fly():void;
}
}

实现:

package com.syx.flex.test{
public class Bird implements IFlyable {
public function Bird() {

}

public function fly():void {
trace("bird fly!");
}
}

}

多态:

      var f:IFlyable = new Bird();

f.fly();

5 简单字符串处理

var s:String = "syx";
for(var i:int=0; i<s.length; i+=1) {
trace(s.charAt(i), s.charCodeAt(i) );
}
trace(s.concat(" hellow", " world"));
trace(s.toUpperCase());

6 数组

var a1 : Array = [1, 2, 3];
var a2 : Array = ["a", "b", "c"];
var a3 : Array = new Array();
trace(a3.push("one"));//1
trace(a3.push("two"));//2
trace(a3.push("three"));//3
trace(a3);//one,two,three
var a4 : Array = new Array(3);
trace(a4.length);//3
trace(a4[0]);//undefined
var a5 : Array = new Array('zhangsan', 'lisi', 'wangwu');
a5.unshift('zhaoliu');
trace(a5);//zhaoliu,zhangsan,lisi,wangwu
//function splice(startIndex:int, deleteCount:uint, ... values):Array
a5.splice(1, 0, 'sunqi', 'liuba');
//删除1位置后0个元素(包括1),在插入...args
trace(a5);//zhaoliu,sunqi,liuba,zhangsan,lisi,wangwu
a5.splice(1, 2);
trace(a5);//zhaoliu,zhangsan,lisi,wangwu
//Removes the last element from an array and returns the value of that element
a5.pop();
//Removes the first element from an array and returns that element.
a5.shift();
delete a5[0]; //a5[0] = 'undefined'
trace(a5[0]);//undefined
//Reverses(逆序) the array in place
a5.reverse();
//Sorts the elements in an array. This method sorts according to
//Unicode values. (ASCII is a subset of Unicode.)
a5.sort();
a5.sort(Array.CASEINSENSITIVE);
a5.sort(Array.DESCENDING | Array.CASEINSENSITIVE );

var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});
poets.sortOn("born", Array.NUMERIC);

7 异常处理

var a :Array = [1, 2, 3];
try {
throw new EOFError("error occur");
} catch (error:EOFError) {
trace(error);
} finally {
trace("finally");
}
posted @ 2011-04-19 16:32  BuildNewApp  阅读(8405)  评论(1编辑  收藏  举报