AS3 ByteArray to an object using depth copy

Use ActionScript3 new in ByteArray class (flash.utils.ByteArray) can create an object of deep copy. "Depth" means that you can copy an object (object) of all references, which means that if you copy an array containing an object, then the array of objects are copied (not quoted). Here is a clone method:

ActionScript Code  :

functionclone(source:Object):*{
var copier:ByteArray=newByteArray();
copier.writeObject(source);
copier.position=0;
return(copier.readObject());
}

Note: you need to import flash.utils.ByteArray;

To use this method, you can use the following code:

ActionScript Code:

newObjectCopy=clone(originalObject);

Although this method you can copy the depth of the object, but it will not copy with the class type definition. So, if Charlie need to copy a MyClass instance, copy the object will not be MyClass type. This method is best to use ordinary objects.

Here are some of my test code:

i

mportflash.utils.ByteArray;
flash.utils.getQualifiedClassName;
var a1:Array=[1,2,3];
var a2:Array=clone(a1);// Clone a1, a2 is saved in  a1 Of copies, and  a1 There is no relationship between the
var a3:Array=a1;// Because of the a1 is an array  , So the a3 is a reference  a1
a1.push(4);// Change the value of the a1
trace(a1);// Output  1,2,3,4
trace(a2);// Output  1,2,3
trace(a3);// Output  1,2,3,4
trace("===============");
varaa1:Array=[1,2,3];
varaa2:Array=[11,22,aa1]//aa2 An element is in the  aa1
varaa3:Array=clone(aa2);// Clone aa2, is saved in aa3  aa2 A copy, of aa2  aa1 Also copies
trace(aa1);// Output  1,2,3
trace(aa2);// Output  11,22,1,2,3
aa1.push(4);// Modify the value of aa1
trace(aa2);// Output  11,22,1,2,3,4
trace(aa3);// Output  11,22,1,2,3
trace(getQualifiedClassName(aa2));// Output  Array
trace(getQualifiedClassName(aa3));// Output Array, this description  Array You can maintain a clone of a clone of the result Array type invariant
trace("===============");
var aaa1:MyClass=newMyClass();// Build a custom type
var aaa2=clone(aaa1);// Clone custom types
trace(aaa2.a);// Output  1
trace(getQualifiedClassName(aaa2));// This indicates that the output Object,  aaa2 The type of MyClass lost
functionclone($source:Object):*
{
var _copier:ByteArray=newByteArray();
_copier.writeObject($source);
_copier.position=0;
return_copier.readObject();
}

MyClass.as content

package{
public class MyClass{
public var a:int=1;
public var b:int=2;
public function MyClass(){}
}
}

Note:
Can not be cast for the aaa2 type, if you use the following statement:

var aaa2=MyClass(clone(aaa1));
 It is an error  :

TypeError:Error#1034: Cast type failed  : Cannot add  Object@138c0341 Converted to  MyClass.
atDeepObjectCopieswithByteArray_fla::MainTimeline/DeepObjectCopieswithByteArray_fla::frame1()
 If you use the convert statement as  , The resulting value is aaa2  null.
分类:Flash 时间:2010-05-31 人气:219
分享到:
blog comments powered by Disqus

相关文章

  • Using pojo object data source for birt 2010-03-29

    Using pojo object data source for birt * Create a POJO object: package org.zsl.pojo; / ** * Define a simple POJO class * Casually defines several attributes which * / public class User ( private String name; private String phone; private String addre

  • Db4o open source object-oriented database Journey, Part 1: acquaintance db4o 2010-02-25

    Db4o open source object-oriented database Journey, Part 1: acquaintance db4o Document options Print this page Send as e-mail this page Levels: Rosen Jiang (rosener_722@hotmail.com), software engineers, db4o and loyal fans OO Chang Huang visions (zhan

  • Oracle objects - abstract data types. Variable array. Nested table. Object tables and object views 2010-02-23

    Oracle Database is RDBMS (relational database) is ORDBMS (object-oriented database). RDBMS: When reusing a function, will need to be compiled; ORDBMS: allows you to define object types; in a relational database to store complex business models; devel

  • Object-Oriented javascript - object-wide solution 2009-04-08

    Belong to reprint this article http://www.javaeye.com/topic/345641 Author: Topcss QQ: 419074376 E-mail: andpai1.0 @ gmail.com Time :2008-11 month Background: JavaScript quick contacts for two years. From the start of her full of curiosity, and now a

  • dom object and jquery object 2008-11-12

    Difference dom object: Through the javascript in the geElementsByTagName or getElementById to get the elements of Node, so get dom element is the dom object. Dom object can use the javascript in the method var domObj = docuemnt.getElementById("id&quo

  • Object-oriented or object-oriented! 2010-03-12

    In today's world of software development, object-oriented has been leading technologies, including OOA (Object Oriented Analysis), OOD (Object Oriented Design), OOP (Object Oriented Programming). The code fragments from specific implementation, to de

  • dom jQuery object and the object of mutual transformation 2010-07-06

    Just started learning jQuery, may be you could not tell what a moment jQuery object, which is a DOM object. The DOM object much to explain, we have come too much, concentrate on the following jQuery, and the conversion between the two. What is a jQue

  • JS to create the object create the object (switch) 2010-09-26

    In JS, create an object (Create Object) is not exactly what we often said that the creation of class objects, JS objects in stress is a complex type, JS to create an object and the object of the visit is extremely flexible. JS object is a complex typ

  • Object-oriented and object based 2011-01-04

    First, some misunderstandings of object-oriented concepts "Object-oriented" is now being called a bad words, just like last year, people like to own company branded ". Com" as the tag. In fact, how many people can really understand wha

  • Java: use of synchronized and the Lock object for the object lock (turn) 2010-11-02

    Transfer from: http://zhangjunhd.blog.51cto.com/113473/70300 In a concurrent environment, sharing resources to resolve conflicts, you can consider using the lock mechanism. 1. Objects are automatically lock all the objects with a single lock. JVM is

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.182 (s). 12 q(s)