本文介绍了对象的闪存复制 - 克隆库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的问题,我只是不知道如何去做.

This is probably a very simple question, I just don't have the foggiest how to go about it.

我有一个想要复制的对象,但不知道如何去做.这是我的尝试:

I have an Object that I want to duplicate, and don't know how to go about it. Here's my attempt:

var myObj = new ObjectClass();
var duplicate = myObj;
duplicate = null;
myObj.function(); // Error: Null reference

ObjectClass 非常大,它继承并创建了它自己的子类,而且我确信其中可能有一些单例类.

The ObjectClass is very large, inherets and creates children of it's own, and I'm sure there's probably a few singleton classes in there.

有没有办法轻松复制东西?

Is there a way to duplicate something easily?

看起来我正在寻找克隆",它没有 AS3 功能,而且您显然无法克隆私有数据.有人知道克隆一堆私人数据的库或解决方法吗?

Looks like I'm looking for "Cloning", for which there is no AS3 function, and you apparantly can't clone Private data anyway. Anyone know of a library or a workaround for cloneing a bunch of private data?

推荐答案

我从某个博客上得到了这个 util 函数,不记得从哪里来的,所以我不能给予信任.但是它不适用于位图数据.无论如何,它是:

I got this util function from some blog, can't remember from where so I can't give the credit. It wouldn't work with bitmapdata though. Anyway here it is:

public static function copy(o:Object):Object
{
    var bytes:ByteArray = new ByteArray( );
    bytes.writeObject( o );
    bytes.position = 0;
    return bytes.readObject( );
}

用法:
registerClassAlias("com.tests.TestClass", TestClass);var testCopy:TestClass = TestClass(ObjectUtil.copy(test));

Usage:
registerClassAlias("com.tests.TestClass", TestClass);var testCopy:TestClass = TestClass(ObjectUtil.copy(test));

这篇关于对象的闪存复制 - 克隆库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:09