as学习笔记(十九)--as3中的Class对象和Function对象
sshong 发表于2008年7月6日 11:21:00 更新于2008年7月6日 11:21:00
as3是完全的面向对象,Class和Function类都是Object的子类,而且都是dynamic动态类!而且一旦我们定义了一个类,定义了一个函数,那么我们就生成了一个Class实例和一个Function实例!

有两种方式来得到Class实例和Function实例
一种直接定义一个class或者一个function,这样类名、函数名自动成为Class实例和Function实例
还有一种如同定义实例一样,将一个Class引用(一个类名)或者Function(一个函数名)赋给变量(增加了引用计数)

一、Class

首先我们不能混淆Class实例和某class实例
通俗的讲定义类时的类名就是代表一个Class实例,而用var、new关键字定一个变量时则声称了一个某class实例
如:
class Foo{}
var instance:Foo = new Foo();
其中Foo是一个Class类的实例,而instance则是一个Foo类的实例,其实遥想当年定义as3的Class.as文件中,class Class{}时,其实Class也是一个Class类的实例,哈哈
trace(Math is Class);//true
trace(Class is Class);//true

那么这个Class实例有些什么属性,什么方法呢?
它包含了类定义中的static方法,static属性!
所以说Class是一个dynamic,它根据类定义的不同来生成实例。
譬如
class A
{
static public var url:String = "www.asarea.me";
}
class B
{
static public var name:String = "shushenghong";
}
我们此时已经生成了两个Class实例,变量名一个叫A,一个叫B。虽然都是Class的实例,但是拥有不同的属性!当然这个属性和方法赋予过程是as编译时自动添加的。在外部就像调用普通实例一样用“.”号调用它的属性,如A.url。

既然是实例,也存在着什么时候被gc回收的问题,我也在想gc何时回收Class的实例呢?

哈哈,其实永远不会回收!因为一旦我们在声明一个Class时,已经定义了一个Class实例,它的引用个数reference count就是1,当然如果其它地方继续引用了这个Class,譬如在某些地方 var alias:Class = A;那么A的引用个数就是2了,但是永远不会小于1拉!

了解这些有什么用啊?
看段代码
var MyMath:Class = Math;
MyMath.GS = 0.618;//黄金分割
trace(MyMath.GS);//0.618
trace(MyMath.PI);//3.14....

看见没,A是一个Class实例,它本身拥有Math这个类定义时的static方法和static属性!我们不能往Math中加属性,加方法,但是我们可以往MyMath中加!
大家再接着研究,有什么好的妙用记得回复就是。


二、Function,黑羽师兄已经研究了很多妙用了

Function同样在定义的时候就生成了一个Function实例,Function同样是dynamic的。
Function有不同的定义方法,一种是表达式定义,一种是标准的语句定义法,这里不研究表达式法,只研究标准的语句定义法。

我们在定义function func(){}时,func即为一个Function的实例(确切的讲是MethodClosure的实例),这个实例拥有什么属性和方法呢,一方面有Function的arguments,apply等,另一方面,Function是动态的,如果定义时用的...params,那么就会自动拥有一个params属性,当然我们可以手工加属性进去。
关于应用,看看殿堂之路把。

Function由于一方面可以作为类的方法,一方面可以作为一个函数闭包,所以其意义也有所不同。

如果作为一个函数闭包,那么同类一样,在定义时,即保证了该Function的实例永远有一个引用。(注,如果是表达式法定义即匿名函数,则不会这样!)

如果作为一个类的静态方法,那么因为Class实例存在,它的引用个数永远不会小于1,它也永远存在。这也是为什么可以在任何地方都可以获取到类的静态方法和静态属性的原因。

如果作为一个普通的类方法,那么在类没有实例化以前,该Function实例也不会存在,更不用提引用了!一旦类实例化了,这个Function实例存在了,并且与对应的实例所关联,它的引用次数随情况而定。因为可能在别的地方会有这个Function实例的引用,譬如把objA.addlistener(objB.func),因为objA中有对objB的func这个函数实例引用。


三、英文摘录:摘自as3语言和组件参考V3.0

1.A Class object is created for each class definition in a program. Every Class object is an instance of the Class class. The Class object contains the static properties and methods of the class. The class object creates instances of the class when invoked using the new operator.
Some methods, such as flash.net.getClassByAlias(), return an object of type Class. Other methods may have a parameter of type Class, such as flash.net.registerClassAlias().

The class name is the reference to the Class object, as this example shows:

class Foo {}
The class Foo{} statement is the class definition that creates the Class object Foo. Additionally, the statement new Foo() will create a new instance of class Foo, and the result will be of type Foo.

2.A function is the basic unit of code that can be invoked in ActionScript. Both user-defined and built-in functions in ActionScript are represented by Function objects, which are instances of the Function class.
Methods of a class are slightly different than Function objects. Unlike an ordinary function object, a method is tightly linked to its associated class object. Therefore, a method or property has a definition that is shared among all instances of the same class. Methods can be extracted from an instance and treated as "bound" methods (retaining the link to the original instance). For a bound method, the this keyword points to the original object that implemented the method. For a function, this points to the associated object at the time the function is invoked.
标签:asClass对象Function对象分类:As3&Flex阅读:4535
评论
暂无评论
添加评论
您的大名,限长10汉字,20英文(*)
电子信箱(*)
您的网站
正文,限长500汉字,1000英文(*)
验证码(*) 单击刷新验证码
联系我
博客订阅