ActionScript3: Dynamic Class
- 博客分类:
- 技术杂绘
ActionScript3: Dynamic Class
2011年04月07日
In Java, if you’ve created an object from a particular class, you can use only properties and methods that were defined in this class. For example, if the following class: class Person {
String name;
} you can only manipulate with the name property: Person p = new Person();
p.name = “Joe”;
System.out.println(p.name); ActionScript calls such classes sealed, but it also has different animals: dynamic classes, which allow you to programmatically add new properties and behavior to classes during the run-time. Just add the magic keyword dynamic to the class definition: dynamic class Person {
var name:String;
} Now let’s add dynamically two variables name and age and the function printme() to the object of type Person: Person p= new Person();
p.name=”Joe”;
p.age=25;
p.printMe = function () {
trace (p.name, p.age);
}
p.printMe(); // Joe 25 You do not have complete freedom though: you can dynamically add only public properties and methods. Of course, nothing comes for free and sealed classes are a bit more efficient in terms of memory consumption, because they do not need to create a hash table to store the properties and methods that are unknown during compilation. Another obvious restriction is that dynamically added functions can’t access private members of the dynamic class. Read the article Programming In Style or an Elevator Pitch to see how by just declaring standard Flex component dynamic, the your code becomes more simple and elegant. In AS3, any function can be attached to a dynamically created property of a dynamic object, for example function calcTax():Number {…} var myObject:SomeObject = new SomeObject();
myObject.tax=calcTax; //add the tax property and attach the function calcTax()
var myTax=myObject.tax(); The delete operator destroys the property of an object and makes it eligible for garbage collection: delete calcTax();
myTax=myObject.tax() // generates an error Some of the Flex classes were defined as dynamic, i.e. Object, Array, MovieClip, NetConnection, TextField, and others. At the time of this writing, subclasses of dynamic classes are not dynamic by default.
Because of this, you may run into an ugly run-time error: imagine a sealed class S that extends a dynamic class D. If you create an object as
D myObj = new S(), an attempt to add a propery to myObj will produce a runtime error because the variable myObj points at a sealed object. Let’s do a quick test. Create a new project in FlexBuilder and select ActionScript project as its type. Enter AS_Only_Project as the project name. In a couple of seconds you’ll see the auto-generated code that looks as follows: package {
import flash.display.Sprite; public class AS_Only_Project extends Sprite
{
public function AS_Only_Project()
{
}
}
} Next, create a new class called D and check odd the Dynamic checkbox in FlexBuilder pop-up. You’ll get this class. package {
public dynamic class D
{
}
} Now, instantiate and test the dynamic nature of the class D by adding the constructor
public function AS_Only_Project()
{
var myD:D=new D();
myD.favoriteBand=”Pink Floyd”;
trace(”Favorite Band=”+myD.favoriteBand);
}
Run this application in the debug mode, and sure enough it’ll print
Favorite Band=Pink Floyd
Create one more sealed class called S inherited from the dynamic D:
package {
public class S extends D
{
}
}
An attempt to add properties on the fly to the instance of the class S fails miserably as expected:
var myS:D = new S();
myS.favoriteSinger=”Alla Pugacheva”;
trace(”Favorite Singer=”+myS.favoriteSinger);
ReferenceError: Error #1056: Cannot create property favoriteSinger on S.
at AS_Only_Project$iinit()[C:\TheRIABook\eclipse\AS_O nly_Project\AS_Only_Project.as:13]
If you’ll try to instantiate your sealed class as follows:
var myS:D = new S() as D;
I have two news for you: the good news is that it compiles, and the bad (and expected) news is that it generates exactly the same runtime error.
Most likely Adobe’s gonna hire a hitman and kill me after the following statement, but I’m going to say it anyway (at least you’ll now who to blame)… May be I should not?…Life is so good, and I’d like to witness the success of Apollo…I’m sayyyiiiinng this:
If you need to add new functionality to one of the existing standard Flex components (buttons, comboboxes and the like), do not bother extending them and creating new classes. Just create one simple empty subclass with the keyword dynamic and instantiate and add new properties on the fly as needed, as was shown by the Smalltalk student in this article.
A sound of a silenced pistol shot. Curtain.
Yakov Fain
发表评论
-
windows mobile activex and atl create (from csdn)
2012-01-20 09:25 604windows mobile activex and atl ... -
以远程桌面访问Windows Azure虚拟机(转+译)
2012-01-20 09:25 836以远程桌面访问Windows Az ... -
myeclipse 配置android SDK 出现 could not find adb.exe 和 tools 找不到的问题
2012-01-20 09:25 795myeclipse 配置android SDK 出现 coul ... -
更新:来自MIX11的Windows Azure新闻
2012-01-20 09:25 600更新:来自MIX11的Windows Az ... -
Android环境配置与HelloWorld程序(Windows)
2012-01-20 09:25 641Android环境配置与HelloWorl ... -
那些年,那些人,那些事
2012-01-19 14:26 500那些年,那些人,那些事 2012年01月14日 ... -
jjk
2012-01-19 14:26 582jjk 2011年12月25日 ... -
端午节的历史与风俗详考
2012-01-19 14:26 596端午节的历史与风俗详考 2010年06月13日 [摘要] ... -
明史 卷四十七??志第二十三
2012-01-19 14:26 530明史 卷四十七??志第二十三 2011年04月29日 礼 ... -
2011-7-3
2012-01-19 14:26 5432011-7-3 2011年07月03日 电大《汉语基础 ... -
ApplicationDomain学习
2012-01-17 04:15 610ApplicationDomain学习 2011年03月11 ... -
ReferenceError: Error #1069
2012-01-17 04:14 626ReferenceError: Error #1069 20 ... -
【转】 JavaScript 异常 使用throw语句抛出异常顾名思义,就是超出常规,
2012-01-17 04:14 699【转】 JavaScript 异常 使 ... -
2011年9月9日
2012-01-17 04:14 4662011年9月9日 2011年01月15日 重要提醒:系 ... -
赞美女人的词语
2012-01-16 03:04 684赞美女人的词语 2011年02月24日 形容美貌: ... -
赞美客户词汇大全
2012-01-16 03:04 824赞美客户词汇大全 2009 ... -
赞美客户词汇大全
2012-01-16 03:04 808赞美客户词汇大全 2011 ... -
词语的感情色彩
2012-01-16 03:03 1370词语的感情色彩 2009年1 ... -
形容女人漂亮的词语、成语或诗句
2012-01-16 03:03 1291形容女人漂亮的词语、成语或诗句 2010年11月29日 ...
相关推荐
从 ActionScript 2.0 迁移到 ActionScript 3.0:主要概念和变化
用actionscript3.0编写的五子棋游戏实例,希望对初学者有所帮助
AdvancED ActionScript 3.0: Design Patterns AdvancED ActionScript 3.0 is a fresh look and new approach to utilizing valuable, structural techniques and methods that are commonly used in the field of ...
awesome-actionscript3:精选的ActionScript 3和Adobe AIR的库和组件列表
本书是国内第一本“面向原因式”(Why-Oriented Book)、全面系统介绍Flash ActionScript 3的书籍。全书共分为5个部分。第一部分:ActionScript 3语言基础;第二部分:ActionScript 3 面向对象编程;第三部分:...
ActionScript 3 For Adobe Flash CS4 Professional About this guide This guide provides a quick introduction to migrating to ActionScript 3 from ActionScript 2. It is targeted at designers and ...
《Flash ActionScript3殿堂之路》共分5个部分。第1部分:ActionScript 3语言基础;第2部分:ActionScript 3面向对象编程;第3部分:ActionScript 3 核心类;第4部分:ActionScript 3主要的Flash Player API;第5部分...
Foundation ActionScript 3 Animation:Making Things Move!中文版。分两部分上传。史上最好的AS3教程!王汝义翻译~
Foundation ActionScript 3 Animation:Making Things Move!中文版。分两部分上传。史上最好的AS3教程!王汝义翻译~
ActionScript3代码规范 ActionScript3代码规范 ActionScript3代码规范
语言-actionscript3 包 Atom 语言支持。 最初从转换而来 安装 去做 Flash播放器 可以在找到 Flash 播放器的完整存档,需要播放器的调试器版本来跟踪输出和显示运行时错误。 公约 任何 ActionScript 格式都遵循 Adob...
as3 pdf下载 ActionScript 3.0 是一种强大的面向对象编程语言,它标志着 Flash Player Runtime 演化过程中的一个重要阶段。设计 ActionScript 3.0 的意图是创建一种适合快速地构建效果丰富的互联网应用程序的语言,...
ActionScript3中文手册
终极protobuf actionscript项目 归功于 Robert Blackwood,他开始了最初的协议缓冲区动作脚本项目,该项目用作... 已知错误已作为 protobuf-actionscript3-2.3 的一部分进行修复。 2.3 版已上传 。 依赖关系 从 2.0 开
ActionScript3中文教程,详细介绍了用 FLASH CS3开发应用程序。
5)ActionScript3_cookBook_en.chm Adobe Flex4.0 ActionScript3 中文API 语言参考.part1.rar的下载地址: http://download.csdn.net/source/3245173 Adobe Flex4.0 ActionScript3 中文API 语言参考.part2.rar的...
Flex与ActionScript3程序开发》一书由易到难、由浅人深、循序渐进地介绍了Flex与ActionScript3程序开发的一般步骤与常用的程序技巧,内容通俗易懂,使用大量实例贯穿全书,力求以实战的形式让读者快速掌握知识点。...
flash actionscript3 纯AS3代码_资源加载进度条.rar
该控件用Flash CS4+ActionScript3写成,能在网页上方便调用,用FlashVars参数传入图片路径。 该控件涉及了很多AS3画图的知识点。实现了加载图片,过度平滑,能通过调整SliderBar对加载的相片随意放大缩小等功能。对...
★国内第一本“面向原因式”(Why-Oriented Book)与国际同步的、... ActionScript 3语言基础,ActionScript 3面向对象编程,ActionScript 3核心类,ActionScript 3主要的Flash Player API,ActionScript 3视觉编程。