AS3.0 Dynamic class

本文介绍了ActionScript中动态类的概念及使用方式,对比了动态类与密封类的区别,并通过实例展示了如何为动态类实例在运行时添加新的属性和行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_Only_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

o-link : 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc xls。Apache POI 提供了 HSSF XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值