http://bbs.9ria.com/viewthread.php?tid=76150&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000
资讯类型: 翻译
来源页面: http://www.bytearray.org/wp-content/projects/wcydwb/Chapter%202%20-%20Everyday%20bytes.pdf
资讯原标题:
资讯原作者: Thibault Imbert
我的评论:有时间会把后面的也翻译一下,希望大家多拍砖
对这篇文你有啥看法,跟贴说说吧!欢迎口水和板砖,哈哈。欢迎大家和我们一同分享更多资讯。
本帖最后由 sun11086 于 2011-3-15 17:14 编辑
第一节
复制对象
复制对象是ByteArray最常用的用途之一.我们可以通过Bytearray的API对AMF进行序列化和反序列化.writeObject就是我们需要的API:
// creates an empty ByteArray
var stream:ByteArray = new ByteArray();
// creates an object
var parameters:Object = { age : 25, name : "Bob" };
// serializes the object as amf and stores it into the ByteArray
stream.writeObject( parameters );
我们通过API readObject来提取已序列化的实例:
// creates an empty ByteArray
var stream:ByteArray = new ByteArray();
// creates an object What can you do with bytes ? – Chapter 2 – Everyday bytes
var parameters:Object = { age : 25, name : "Bob" };
// serializes the object as amf and stores it into the ByteArray
stream.writeObject( parameters );
// resets the position
stream.position = 0;
// reads the object copy
var objectCopy:Object = stream.readObject();
然后我们可以将下面的逻辑放到一个自定义函数中:
function copyObject ( objectToCopy:* ):*
{
var stream:ByteArray = new ByteArray();
stream.writeObject( objectToCopy );
stream.position = 0;
return stream.readObject();
}
在任何需要的时候,我们可以通过调用copyObject函数来复制对象:
// creates an object
var parameters:Object = { age : 25, name : "Bob" };
var parametersCopy:Object = copyObject ( parameters );
/* outputs :
name : Bob
age : 25
*/
for ( var p:String in parametersCopy )
trace( p, " : ", parametersCopy[p] );
function copyObject ( objectToCopy:* ):*
{
var stream:ByteArray = new ByteArray();
stream.writeObject( objectToCopy );
stream.position = 0;
return stream.readObject();
}
通过修改原始对象的属性,我们可以看到我们已经正确的复制了对象的所有属性:
// creates an object
var parameters:Object = { age : 25, name : "Bob" };
var parametersCopy:Object = copyObject ( parameters );
parameters.nom = "Stevie";
/* outputs :
name : Bob
age : 25
*/
for ( var p:String in parametersCopy )
trace( p, " : ", parametersCopy[p] );
function copyObject ( objectToCopy:* ):*
{ What can you do with bytes ? – Chapter 2 – Everyday bytes
var stream:ByteArray = new ByteArray();
stream.writeObject( objectToCopy );
stream.position = 0;
return stream.readObject();
}
让我们进一步探寻怎么样通过ByteArray的API实现更多复杂类型的存储和提取.
序列化/反序列化 自定义对象
需要注意的是下段代码对自定义类型不起作用.用一个简单的例子来说明:现在你需要使用到如下的一个User对象.
package
{
public class User
{
private var _firstName:String;
private var _lastName:String;
public function set firstName (firstName:String):void
{
_firstName = firstName;
}
public function set lastName (lastName:String):void
{
_lastName = lastName;
}
public function get firstName ():String
{
return _firstName;
}
public function get lastName ():String
{
return _lastName;
}
}
}
也许你需要将User实例存以二进制形式存储到服务器或者存储到本地的SharedObject中.如果你尝试将User的实例存储到ByteArray中并且以后会再次提取它,当读取它的时候,Flash Player将会在内部检索是否已经注册过名为User的类型,如果没有player就会按照Object的类型来反序列法该对象:
// creates an instance of User
var myUser:User = new User ();
// sets the members
myUser.firstName = "Stevie";
myUser.lastName = "Wonder";
// outputs :[object User]
trace ( myUser );
// create a ByteArray to store the instance
var bytes:ByteArray = new ByteArray();
// stores the instance What can you do with bytes ? – Chapter 2 – Everyday bytes
bytes.writeObject ( myUser );
// resets the position
bytes.position = 0;
// outputs : false
trace ( bytes.readObject() is User );
现在,我们通过使用registerClassAlias来告知Flash player并注册User类型,以便Flash Player来自动反序列化User类型的实例.
// creates an instance of User
var myUser:User = new User ();
// sets the members
myUser.firstName = "Stevie";
myUser.lastName = "Wonder";
// outputs :[object User]
trace ( myUser );
// registers the type User for deserialization
registerClassAlias ( "userTypeAlias", User );
// create a ByteArray to store the instance
var bytes:ByteArray = new ByteArray();
// stores the instance
bytes.writeObject ( myUser );
// resets the position
bytes.position = 0;
// reads the stored instance and automatically deserializes it to the User type
var storedUser:User = bytes.readObject() as User;
// outputs : true
trace ( storedUser is User );
// outputs : Stevie Wonder
trace ( storedUser.firstName, storedUser.lastName );
通过这种技术我们可以保存任何自定类型的对象 并且 提取他们.正如你看到的我们将复制一个自定义类型:
storedUser.firstName = "Bobby";
storedUser.lastName = "Womack";
// outputs : Stevie Wonder
trace ( myUser.firstName, myUser.lastName );
// outputs : Bobby Womack
trace ( storedUser.firstName, storedUser.lastName );
需要注意的是一些基础类型是不能被序列化或反序列化为AMF.例如DisplayObject等.所以如果你想将一个MovieClip的实例序列化是行不通的:
// creates a ByteArray
var bytes:ByteArray = new ByteArray();
// try storing a DisplayObject type
bytes.writeObject ( new MovieClip() );
// resets the position
bytes.position = 0;
What can you do with bytes ? – Chapter 2 – Everyday bytes
// outputs : undefined
trace ( bytes.readObject() );
AMF3对显示对象的支持将会是一个非常大的补充.
英文词汇:4780(不计空格)
翻译后词汇:3917(不计空格)
分享到:
相关推荐
外研版(一起)英语二年级上册 Module6 Unit1 What do you do on Sundays?(课件).ppt
这篇文档是针对陕旅版四年级英语教材Unit 8 "What Can You Do"的课堂练习题目及答案。这个单元主要教授孩子们用英语表达自己能做什么技能或者询问他人能做什么,以此来提升他们的日常交流能力。 一、英译汉部分: 1...
这篇文档是关于五年级英语上册 Unit 4 What can you do?第二课时的教学教案,主要针对人教PEP版教材。本课的教学内容包括Part A的Let’s try, Let’s talk, Group work以及Good to know(C)部分,旨在帮助学生掌握与...
新PEP五年级上册What can you do作文This is mePPT学习教案.pptx
Designing Embedded Hardware includes:<br><br><br>The theory and practice of embedded systems<br><br><br>Understanding schematics and data sheets<br><br><br>Powering an embedded system<br><br><br>...
You can round up to the width to the nearest four bytes as follows:<br> (.biWidth * 3 + 3) And &HFFFFFFFC<br> <br><br><br>This allows you to create a DIB Section. You call CreateDIBSection like this:...
Tools and Languages <br> Tools You Cannot Do Without <br> Optimizing with Help from the Compiler <br> The Language for the Job <br> Summary <br> <br> Chapter 5. Measuring Time and Complexity <br> The...
You can round up to the width to the nearest four bytes as follows:<br> (.biWidth * 3 + 3) And &HFFFFFFFC<br> <br><br><br>This allows you to create a DIB Section. You call CreateDIBSection like this:...
Some Final Comments on the MOV Instructions<br><br>4.9 Laboratory ...with MASM<br>4.9.5 Debuggers and CodeView'<br>4.9.5.1 A Quick Look at CodeView<br>4.9.5.2 The Source Window<br>4.9.5.3 The Memory ...
这篇PPT教案是针对五年级英语上册Unit的内容,主题为"What can you do",主要教授学生关于家庭日常家务活动的英语表达。课程的核心在于学习和掌握与家务相关的词汇,并通过猜测游戏等活动来提高学生的语言运用能力。...
外研版(一起)英语二年级上册 Module6 Unit1What do you do on Sundays?(课件).ppt
【人教版小学英语五年级上册 "What_can_you_do" 课程解析】 在小学英语教育阶段,"What_can_you_do?" 这个主题旨在帮助学生掌握与能力相关的英语表达,提升他们的日常交际能力和自我介绍技巧。这个课件是2014年人教...
外研版(一起)英语二年级上册 Module6 Unit1 What do you do on Sundays?_(课件).ppt
Bus hound logs each phase complete with timing information and a description of the content.<br><br>Capture columns<br><br>Below are descriptions of information in each column of the Capture Window:...
For DIHtmlParser, a Custom-Tags' name must begin with a user-define start character just as #like in <#Name Attribute=“Value” />. PHP: ...
Farkas<br>出版商: Peachpit Press (August 9, 2005)<br>内容介绍:<br>如果你认为互联网广播很酷,那么你将经历播客,你将随心所欲地在任何时候,任何地点收听,它将改变你的收听习惯!本书引导你到播客中来,向你...
With this book, you will learn to:<br><br>Flexible, quick and practical requirements that work<br>Save time and develop better software that meets users\' needs<br>Gathering user stories -- even when ...
人教PEP版五年级上册小学英语 Unit 4 What can you do B Let's talk 教学反思.docx