复制/克隆实例的属性?

时间:2013-10-15 10:03:09

标签: actionscript-3 variables properties copy clone

我有一个具有大量属性的displayobject。我想要做的是克隆这个类。我理解不可能在AS3中对自定义类进行深层复制,因此我只需要复制属性,然后通过克隆方法创建一个与原始属性具有相同精确属性的新实例。

我在获取displayobject的所有属性时遇到了一些麻烦。当然,我可以逐个调用每个var并将其放入一个数组中,并将该数组返回到克隆中,如下所示:

function clone():Array
    array.push(displayObject.x);
    array.push(displayObject.y);
    array.push(displayObject.price);
    return array;

但那并不是面向对象的。所以我正在寻找获取所有对象属性的方法,然后我将这些属性放在一个新对象中。

哦,我在AS3上运行Air not Flex,所以我不能使用mx.utils.ObjectUtil,这是一种方法。

2 个答案:

答案 0 :(得分:1)

您无法复制私人财产。您可以使用ByteArray复制属性

Dog是要克隆的目标类

public class Dog  {

    public function Dog()
    {
    }

    public function setName($name:String):void {
        name = $name;
    }

    public var subDogs:Array = [];

    public var age:int;

    private var name:String;
}

以下是如何克隆

var dog:Dog = new Dog();
dog.subDogs.push(new Dog());
dog.setName("puppy");
dog.age = 15;

registerClassAlias("vo.Dog", vo.Dog);
var dogA:Object = CloneUtil.copy(dog);

var d:Dog = copy(dog) as Dog;

private function copy(source:Object):* {

      var ba:ByteArray = new ByteArray();
      ba.writeObject(source);

      ba.position = 0;

      return ba.readObject();
 }

答案 1 :(得分:0)

package
{
    import flash.display.Sprite;
    import flash.utils.Dictionary;
    import flash.utils.describeType;
    import flash.utils.getQualifiedClassName;

    public class DynamicClone extends Sprite
    {
        public function DynamicClone()
        {
            var p1 : Player = new Player;
            p1.name = "Simsoft";
            p1.hiddenAccessor = 10;

            var p2 : * = clone( p1 );

            // p2 is a Player instance
            trace(getQualifiedClassName( p2 )); // Player

            // p1 and p2 are not the same instance
            trace(p1 == p2); // False

            // But public properties are cloned
            trace(p2.name); // Simsoft
            trace(p2.hiddenAccessor); // 10
        }

        public function clone( source : Object, cloneGetSet : Boolean = true) : *
        {
            if( !source)
                return null;

            if( source is Array || source is Vector.<*> || source is Vector.<uint> || source is Vector.<int> || source is Vector.<Number> || source is Vector.<Boolean>)
                return source.concat();

            var newInstance : Object = new ( source.constructor );

            // Copy dynamic var
            for(var dynamicVar : String in source)
                newInstance[ dynamicVar ] = source[ dynamicVar ];

            var typeDescription : XML = describeType( source );

            // Copy variables
            for each(var variable : String in typeDescription..variable.@name)
                newInstance[ variable ] = source[ variable ];

            // Copy getter / setter
            if(cloneGetSet)
                for each(var getset : String in typeDescription..accessor.(@access == "readwrite").@name)
                    newInstance[ getset ] = source[ getset ];

            return newInstance;
        }
    }
}

internal class Player {

    // Will be clone
    public var name : String;

    // Won't be clone
    private var _score : uint;

    // Will be clone throught getter/setter
    private var _hidden : uint;

    // Can be clone because we have a getter and a setter
    public function get hiddenAccessor():uint
    {
        return _hidden;
    }

    public function set hiddenAccessor(value:uint):void
    {
        _hidden = value;
    }
}