• as3 数据深度克隆


    今天闲来无事,为了做一个小功能,牵涉到数据的深度克隆,在折腾再三还是找到了一个最完美的方法(声明:代码都是引用的)。

    第一次用的是byteArray+类注册做的,因为有复杂数据和vector和Array类型,最后克隆出来的数据类型丢失,代码如下:

    1. private static var aliasList:Array = [];
    2.                 /**
    3.                  * 复制一个对象
    4.                  * @param sourceObj        要复制的源对象
    5.                  * @param deep                是否对对象进行AMF序列化以后再深度复制
    6.                  * @return
    7.                  */
    8.                 public static function clone(sourceObj:*, deep:Boolean=true):*
    9.                 {
    10.                         if(!sourceObj)
    11.                                 return null;
    12.                         var lastTime:Number = getTimer();
    13.                         if(deep)
    14.                         {
    15.                                 // 得到  sourceObj的类名
    16.                                 var qualifiedClassName:String = getQualifiedClassName(sourceObj);
    17.                                 if(aliasList.indexOf(qualifiedClassName) == -1)
    18.                                 {
    19.                                         // e.g  com.gdlib.test::RegisterClassAliasTest
    20.                                         var packageName:String = qualifiedClassName.replace("::", ".");
    21.                                         // 得到 sourceObj的类的类定义
    22.                                         var classType:Class = getDefinitionByName(qualifiedClassName) as Class;
    23.                                         // 注册此别名和类
    24.                                         if(classType)
    25.                                         {
    26.                                                 registerClassAlias(packageName, classType);
    27.                                                 aliasList.push(qualifiedClassName);
    28.                                                 trace("register class", packageName);
    29.                                         }
    30.                                         // 注册类公共属性(如果是复合类)
    31.                                         registerVariables(sourceObj);
    32.                                 }
    33.                         }
    34.                         // 复制此对象
    35.                         var b:ByteArray = new ByteArray();
    36.                         b.writeObject(sourceObj);
    37.                         b.position = 0;
    38.                         trace("Clone object takes", getTimer()-lastTime, "ms. ");
    39.                         return b.readObject();
    40.                 }
    41.                 /**
    42.                  * 注册某个类的公共属性(如果是复合类)所属的类的别名.
    43.                  * @param sourceObj
    44.                  */
    45.                 private static function registerVariables(sourceObj:*):void
    46.                 {
    47.                         // 注册类公共属性(如果是复合类)
    48.                         var xml:XML = describeType(sourceObj);
    49.                         var variable:XML;
    50.                         var variableType:String;
    51.                         var variablePackageName:String;
    52.                         var variableClassType:Class;
    53.                         var variableXml:XMLList;
    54.                         if(sourceObj is Class)
    55.                                 variableXml = xml.factory.variable;
    56.                         else
    57.                                 variableXml = xml.variable;
    58. //                        <variable name="point" type="flash.geom::Point">
    59. //                                <metadata name="__go_to_definition_help">
    60. //                                  <arg key="pos" value="209"/>
    61. //                                </metadata>
    62. //                        </variable>
    63. //                        <variable name="name" type="String">
    64. //                                <metadata name="__go_to_definition_help">
    65. //                                  <arg key="pos" value="142"/>
    66. //                                </metadata>
    67. //                          </variable>
    68.                         for each (variable in variableXml)
    69.                         {
    70.                                 variableType = variable.@type;
    71.                                 if(variableType.indexOf("::") != -1)
    72.                                 {
    73.                                         if(aliasList.indexOf(variableType) == -1)
    74.                                         {
    75.                                                 // "flash.geom::Point"        转换为         "flash.geom.Point"
    76.                                                 variablePackageName = variableType.replace("::", ".");
    77.                                                 variableClassType = getDefinitionByName(variableType) as Class;
    78.                                                 // 注册此别名和类
    79.                                                 if(variableClassType)
    80.                                                 {
    81.                                                         registerClassAlias(variablePackageName, variableClassType);
    82.                                                         registerVariables(variableClassType);
    83.                                                         aliasList.push(variableType);
    84.                                                         trace("register variable", variablePackageName);
    85.                                                 }
    86.                                         }
    87.                                 }
    88.                         }
    89.                 }

    于是乎,继续找结果找到了以下的方法:

    首先,这个需要引用类库as3-commons-reflect(比较详细的解说可以看这:http://topmanopensource.iteye.com/blog/491080  下载类库地址:http://www.as3commons.org/as3-commons-reflect/

    /**
    * 深度clone某个对象,包含Vector 和 Array
    */
    public static function clone (_obj : * ): *
    {
    var returnCloneObj :*;
    var objType : Type = Type . forInstance (_obj );
    if (_obj is Vector .<*> || _obj is Array )
    {
    returnCloneObj = new objType . clazz ();
    for each (var eachCloneObj :* in _obj )
    {
    returnCloneObj . push (cloneObj (eachCloneObj ));
    }
    }
    else
    {
    returnCloneObj = cloneObj (_obj );
    }
    return returnCloneObj ;
    }
    /**
    * clone 只能clone简单对象的所有的public属性
    */
    private static function cloneObj (_obj : * ): *
    {
    var objType : Type = Type . forInstance (_obj );
    var cloneObj :* = new objType . clazz ();
    for each (var field : Field in objType . properties )
    {
    if (field is Variable || field is Accessor && Accessor (field ). writeable && field . name != 'prototype' )
    {
    cloneObj [ field . name ] = _obj [ field . name ];
    }
    }
    return cloneObj ;
    }

    /**
    * 深度clone某个对象,包含Vector 和 Array
    */
    public static function clone (_obj : * ): *
    {
    var returnCloneObj :*;
    var objType : Type = Type . forInstance (_obj );
    if (_obj is Vector .<*> || _obj is Array )
    {
    returnCloneObj = new objType . clazz ();
    for each (var eachCloneObj :* in _obj )
    {
    returnCloneObj . push (cloneObj (eachCloneObj ));
    }
    }
    else
    {
    returnCloneObj = cloneObj (_obj );
    }
    return returnCloneObj ;
    }
    /**
    * clone 只能clone简单对象的所有的public属性
    */
    private static function cloneObj (_obj : * ): *
    {
    var objType : Type = Type . forInstance (_obj );
    var cloneObj :* = new objType . clazz ();
    for each (var field : Field in objType . properties )
    {
    if (field is Variable || field is Accessor && Accessor (field ). writeable && field . name != 'prototype' )
    {
    cloneObj [ field . name ] = _obj [ field . name ];
    }
    }
    return cloneObj ;
    }

    终于可以万能的深度克隆各种数据了。

  • 相关阅读:
    .netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析
    .netcore3.1 设置可跨域
    基于MMSE的预测
    概率模型
    [离散时间信号处理学习笔记] 15. 模拟信号的数字处理
    [离散时间信号处理学习笔记] 14. 多采样率信号处理
    [离散时间信号处理学习笔记] 13. 重采样
    [离散时间信号处理学习笔记] 12. 连续时间信号的离散时间处理以及离散时间信号的连续时间处理
    [离散时间信号处理学习笔记] 11. 连续时间信号的采样与重构
    [离散时间信号处理学习笔记] 10. z变换与LTI系统
  • 原文地址:https://www.cnblogs.com/lweinking/p/3616410.html
Copyright © 2020-2023  润新知