AS 数据类型的一些探索(1)

类别:编程语言 点击:0 评论:0 推荐:

AS 数据类型的一些探索(1)

无聊的时候随便看看MM Flash的文档,在Actionscript_ref中看到了int这个关键字,以前因为习惯了C和java的写法,在写AS的时候也会错写到int,我知道它是AS的关键字,但是一直不知道它的作用,这回我仔细的看了看。

int

Availability

Flash Player 4. This function was deprecated in Flash 5 in favor of Math.round().

Usage

int(value)

Parameters

value A number to be rounded to an integer.

Returns

Nothing.

Description

Function; converts a decimal number to an integer value by truncating the decimal value. This

function is equivalent to Math.floor() if the value parameter is positive and Math.ceil() if

the value parameter is negative.

See also

Math.floor(), Math.ceil()

大致意思是,int在Flash Player 4时就已经出现了,Flash 5是用于四舍五入的一个函数,并且与Math.floor(), Math.ceil()有着比较密切的联系。于是,我就想,趁这个空闲有必要去深入学习一下Flash数据类型、以及类型转换方面的知识。

       不管怎么说,int这个东西还是要先试试才可以知道它具体的功用的:

a=2.9;

b=3.1;

c=-2.9;

d=-3.1;

 

trace("int(a)="+int(a)+";int(b)="+int(b));

trace("int(c)="+int(c)+";int(d)="+int(d));

 

输出

int(a)=2;int(b)=3

int(c)=-2;int(d)=-3

看来并非是四舍五入哦,奇怪了。在金山糍粑中有一行解释了round这个词:弄圆, 使成圆形, 绕行, 完成, 围捕, 四舍五入。看来这里不好把round解释为四舍五入,应该是“弄圆”引申一下就是弄完整,再引申一下就是求整,于是,我这里得出了结论:

int函数是用来小数求整的。

 

Flash的基本数据类型:

       Flash包含以下几种最基本的数据类型:String ,Number,Boolean,Object, MovieClip,Null, Undefined。String就是字符串。Number就是数值类型。Boolean是逻辑类型Object就是object类型(白说),它是一组属性的集合。MovieClip是在AS中唯一包含图形元素的数据类型。Null数据类型仅有一个值null。表示变量还没有受到数据,变量还未赋值等。Undefined数据类型也只有一个undefined值,用于一个变量还未被赋值,听起来似乎和null差不多。不过等一下还是要做一些测试的,看看到底有什么不同。不过首先要来看看一个比较有用的函数typeof,这个函数可以用来确定变量到底是什么类型的。看看测试代码:

var a:String;

var b:Number;

var c:Boolean;

var d:MovieClip;

var e:Object;

 

trace("Type_a="+typeof(a));

trace("Type_b="+typeof(b));

trace("Type_c="+typeof(c));

trace("Type_d="+typeof(d));

trace("Type_e="+typeof(e));

 

a="LeeFJ";

b=22;

c=true;

d=_root.attachMovie("leaf","f",4);

e={name:"LeeFJ"};

trace("+++++++++++++++++++++++++++++++++");

trace("Type_a="+typeof(a));

trace("Type_b="+typeof(b));

trace("Type_c="+typeof(c));

trace("Type_d="+typeof(d));

trace("Type_e="+typeof(e));

输出结果:

Type_a=undefined

Type_b=undefined

Type_c=undefined

Type_d=undefined

Type_e=undefined

+++++++++++++++++++++++++++++++++

Type_a=string

Type_b=number

Type_c=boolean

Type_d=movieclip

Type_e=object

看起来有但奇怪

var a:String;

var b:Number;

var c:Boolean;

var d:MovieClip;

var e:Object;

似乎只是对变量做了一个声明,并没有确定变量的类型,不对,声明变量以后是确定变量为Undefined类型,只有对变量赋值以后,变量才有了他声明中所对应的数据类型。这个不难联想到AS2中的自动数据类型和严格数据类型。我想,严格数据类型也是基于自动数据类型的,否则为什么变量刚声明时是Undefined类型的呢。不过综上,可以得到的结论是:变量在刚声明的时候是Undefined类型的,不管是严格数据类型的定义还是自动数据类型的定义,但是在对变量赋值以后,也就是在对变量分配了内存空间以后,变量就获得了声明语句中所指定的那种数据类型。

这个结论应用到函数中也是一样的 道理:

function test(a:String,b:Number,c:Boolean,d:MovieClip,e:Object)

{

       trace("Type_a="+typeof(a));

       trace("Type_b="+typeof(b));

       trace("Type_c="+typeof(c));

       trace("Type_d="+typeof(d));

       trace("Type_e="+typeof(e)); 

}

test();

trace("+++++++++++++++++++++++++++++++");

test("",123,true,_root.attachMovie("leaf","f",4),{name:"Leefj"});

输出:

Type_a=undefined

Type_b=undefined

Type_c=undefined

Type_d=undefined

Type_e=undefined

+++++++++++++++++++++++++++++++

Type_a=string

Type_b=number

Type_c=boolean

Type_d=movieclip

Type_e=object

但,问题是,什么时候才会出现null值呢? 哈哈仔细看看再说。


点击浏览该文件

本文地址:http://com.8s8s.com/it/it24045.htm