`

AS3中Object与Dictionary的区别

 
阅读更多
AS3中的Dictionary类(flash.utils.Dictionary)是一个新的AS类。

Dictionary类和Object唯一的区别在于:

Dictionary对象可以使用非字符串作为键值对的键,而Object只能使用字符串做为键值对的键。


以下用两个简单例子来说明具体的差别:

1、使用Object: 

使用Object
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->var a:Object = {val:135}
var b:Object = {val:248};   

var obj:Object = new Object();   
obj[a] 
= 1;        // 键为对象a,值为数字1
obj[b] = 2;        // 键为对象b,值为数字2

for(var p:Object in obj)
{
    trace(p);        
// 输出键
    trace(obj[p]);    // 输出此键对应的值
}

 

输出结果:
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->[object Object]
2

 

结果分析:
因为前面说了Object的键只能使用字符串,所以当我们使用Object来做键的时候,都会被转为[object Object],
obj[a] = 1;
obj[b] = 2;
所以在这里,这两句代码的实际效果是这样的
obj["[object Object]"] = 1;
obj["[object Object]"] = 2;
当执行第二句代码的时候,实际上就是更新前面一个的值,所以才输出这样的结果也就不奇怪了。


2、使用Dictionary:
使用Dictionary
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->var a:Object = {val:135}
var b:Object = {val:248};   

var obj:Dictionary = new Dictionary();   
obj[a] 
= 1;        // 键为对象a,值为数字1
obj[b] = 2;        // 键为对象b,值为数字2

for(var p:Object in obj)
{
    trace(p);        
// 输出键
    trace(obj[p]);    // 输出此键对应的值
    trace(p["val"]); // 输出键(Object)的val属性的值
}

 

输出结果:
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->[object Object]
1
135

[object Object]
2
248

 

结果分析:

因为Dictionary和Object的区别所在,可以使用任意类型数据做键,所以能得到我们想要的结果。

 

所在在平时我们使用Object或其子类的时候,如果键是字符串,也可以达到键值对的效果,

但当键需要Object时,就需要Dictionary了。

分享到:
评论

相关推荐

    字典语法基础

    Dim d As New Dictionary Set d = CreateObject("scripting.Dictionary") d.Add "张三", "123" d.Add "李四", "456" '对象.新增 Key,Item '注意:在本地窗口中可能看不到Item条件。 'Keys方法 '返回一个数组,...

    计算重复属性

    Static d As Object Static i As Long Dim iDup As Integer Dim sField ' ---------------------------------------- '这里填写需要检查的字段名 sField = [A] ' ---------------------------------------- If ...

    autocad objectarx类的方法属性清单

    ExtensionDictionary AS Autodesk.AutoCAD.DatabaseServices.ObjectId 可读不可写 FileName AS System.String 可读可写 FlagBits AS System.Byte 可读可写 Font AS Autodesk.AutoCAD.GraphicsInterface....

    K3BOS审批流接口参考.doc

    K3BOS审批流接口参考 K3Lib As K3... DataSrv As Object 说明:单据对象的服务对象,对应组件K3ClassTpl.dll的类DataSrv,提供元数据服务  Template As KFO.Dictionary 说明:当前单据使用的审批流程的模板信息

    java关于字符串拼接的笔试题-as2cs:将一些语法从ActionScript转换为C#的小例子

    关于java习惯的笔试题as2cs 将一些语法从 ActionScript 3 转换为 C# 再从 C# 转换回 ActionScript 3 的小例子。比较兼容的语法。 使用 SimpleParse 的 Typographify 示例来自 David Mertz 受版权保护的文章,在该...

    Rave Reports v11.0.5 Installer Full Source for D7-DXE5

    在Rave Visual Designer窗口左边的Project Tree中,展开Data View Dictionary结点,燃后展开刚创建的DataView1结点. 你要使用的数据字段显示在DataView1结点下. 选择Tools|Report Wizards|Simple Table显示Simple ...

    Nevrona.Rave.Reports.BEX.v11.0.3

    3.在Object Inspector中,设置DataSet的属性为一个已在你的应用程序中定义的dataset组件.使用Rave Visual Designer 4.去设计你的报表然后并创建一个report project文件(.rav file). 选择Tools|Rave Designer运行...

    HashTable、HashSet和Dictionary的区别点总结

    今天又去面试了,结果依然很悲催,平时太过于关注表面上的东西,有些实质却不太清楚,遇到HashTable和Dictionary相关的知识,记录下来,希望对后来人有所帮助,...Hashtable中key-value键值对均为object类型,所以Hasht

    CANopen CiA 301 version 4.2.0 官方最新标准

    CANopen CiA 301 version 4.2.0 官方最新标准 截止2018-12, 发布... This includes the data types, encoding rules and object dictionary objects as well as the CANopen communication services and protocols.

    orcale常用命令

    select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status; 8、查看数据库的版本 Select version FROM Product_component_version Where SUBSTR(PRODUCT,1,6...

    Oraclet中的触发器

    行触发器和语句触发器的区别表现在:行触发器要求当一个DML语句操走影响数据库中的多行数据时,对于其中的每个数据行,只要它们符合触发约束条件,均激活一次触发器;而语句触发器将整个语句操作作为触发事件,当它...

    CANopen培训资料[精选].doc

    the Object Dictionary serves as aninterface between the communicationand the application. 对象字典中索引的分类 3. NMT状态机 " " " " " "过程数据对象(PDO"否 "是 "否 " ") " " " " "服务数据对象(SDO"是 ...

    A Stochastic Grammar of Images

    It formulates each object category as the set of all possible valid configurations produced by the grammar. (ii) The grammar is embodied in a simple And–Or graph representation where each Or-node ...

    CANopen应用层通信协议

    This includes the data types, encoding rules and object dictionary objects as well as the CANopen communication services and protocols. In addition, this specification specifies the CANopen network ...

    C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解

    但是ArrayList也有一个缺点,就是存入ArrayList里面的数据都是Object类型的,所以如果将值类型存入和取出的时候会发生装箱、拆箱操作(就是值类型与引用类型之间的转换),这个会影响程序性能。在.Net 2.0泛型出现以后...

    Progress/OpenEdge语言手册

    * A dictionary of ABL handle-based object events. * A dictionary of ABL classes and interfaces. * A dictionary of ABL properties and methods (for classes). * A dictionary of ABL class events and...

    小学三科成绩统计表.xlsm

    三科成绩统计表 Sub aaa() If Sheets.Count &gt; 2 Then ...Dim d As Object Dim arr As Variant Dim brr() Set d = CreateObject("Scripting.Dictionary") ………………………………………………

    BobBuilder_app

    I decided against using SortedDictionary for the pages as it was slower than a normal Dictionary and for the purpose of a key value store the sorted-ness was not need and could be handled in other ...

    CANopen_meno.pdf

    CANopen_memo,德国工程师的 CANopen 备忘录。包括Object dictionary(OD)对象字典

    JSONBuilder - C#扩展版

    - 支持常见数据类型、以及常用的数据结构如: 任意数组,ArrayList,List&lt;object&gt; ,Hashtable,Dictionary,object&gt; 等 - 支持任意扩展,通过注册自定义类型的转换方法(参看JSONBuilderTest.cs 和 ...

Global site tag (gtag.js) - Google Analytics