如何克隆Flex中的对象? [英] How to clone an object in Flex?

查看:207
本文介绍了如何克隆Flex中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想克隆一个Canvas对象,其中包含了Degrafa表面与几个几何形状。

我尝试了天真的做法:

 返回ObjectUtil.copy(graph_area)作为画布;
 

这导致错误:

 类型错误:错误#1034:类型强制失败:无法转换对象@ 63b1b51到com.degrafa.geometry.Geometry。
类型错误:错误#1034:类型强制失败:无法转换对象@ 63b1039到com.degrafa.geometry.Geometry。
类型错误:错误#1009:无法访问空对象引用的属性或方法。
    在mx.core::Container/addChildAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2196]
    在mx.core::Container/addChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2140] ...
 

解决方案

你想被称为深拷贝什么,产生与原来相同的信息,一个新的实例。

我知道如何将它使用的ByteArray如下唯一能做的:

 私有函数克隆(来源:对象):*
{
    VAR缓冲区:ByteArray的=新的ByteArray();
    buffer.writeObject(源);
    buffer.position = 0;
    返回buffer.readObject();
}
 

AS3真的是缺乏Object.clone()...

I want to clone a Canvas object, which contains a Degrafa Surface with several Geometry shapes.

I tried the naive approach:

return ObjectUtil.copy(graph_area) as Canvas;

which resulted in errors:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@63b1b51 to com.degrafa.geometry.Geometry.
TypeError: Error #1034: Type Coercion failed: cannot convert Object@63b1039 to com.degrafa.geometry.Geometry.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.core::Container/addChildAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2196]
    at mx.core::Container/addChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2140] ...

解决方案

What you want is called a deep copy, generate a new instance with the same information of the original.

The only way I know how to do it is using ByteArray as follows:

private function clone(source:Object):*
{
    var buffer:ByteArray = new ByteArray();
    buffer.writeObject(source);
    buffer.position = 0;
    return buffer.readObject();
}

AS3 is really lacking Object.clone()...

这篇关于如何克隆Flex中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆