加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex入门--Flex基本语法

发布时间:2020-12-15 03:51:17 所属栏目:百科 来源:网络整理
导读:1变量:int/uint/boolean/Number/String/* var b:Object/Array ?--8个 2flex的四种循环语句--针对list/map 3定义函数 数组的单个变量可以为函数 4类/接口/字符串处理/ 1 Flex 变量 ?? ? ? ? ?var ? v1: int ? = ? 9 ;? //int v1 = 9; ?????????? var ? v2: u

1变量:int/uint/boolean/Number/String/*
var b:Object/Array ?--8个
2flex的四种循环语句--针对list/map
3定义函数
数组的单个变量可以为函数
4类/接口/字符串处理/

1 Flex 变量

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

2 Flex中语句

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

for?each?(propertyValue:{
????propertyValue);v2:id:1name:lisi'age:19};
i?v2)?i);????????//name id age
????v2]);????//lisi 1 19
propertyValue?propertyValue);?????//lisi 1 19?
"name"]);????????????//lisi

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

??for?each是取出value

3 函数

?? ?public?function?AS_0400_Functions()

//声明一个匿名函数类型变量traceParameter,定义在后面
????traceParameter:Function?function?? (aParam:String)?:void//void f(String aParam)
????{
????????aParam);
????};
????traceParameter("hello");?// hello
????
????
????traceArray:new?Array();?//定义一个数组
????//数组的0号位置存储了一个函数变量,匿名函数变量,带一个String类型参数
????traceArray0]?function?(String):void?
????]("hello");
????
????f();
????f(');
????
????f2(68);
????
????arr?"hello world!""syx"];
????say(arr"test"]);
}
//函数 可以有默认参数值
f(name:zhangsan')?:?void?name);
}

//函数可以有变参
//args 是一个存储Object类型的数组
f2(...args):?{
????
????argslength);
????each(value:args)?value);
????}
saystrTemp?"";
????value?+=?+?":";
????}
????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;
}
}
接口:

interface IFlyable {
function fly():void;
}
}

实现:

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,'sunqi','liuba');
删除1位置后0个元素(包括1),在插入...args
trace(a5);a5.splice(1,2);
trace(a5);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,3];
try {
throw new EOFError("error occur");
} catch (error:EOFError) {
trace(error);
} finally {
trace("finally");
}

复制代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读