`

[转] <What can you do with bytes ?> 第二章 第一节: 对象的复制

阅读更多
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(不计空格)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics