虽然看起来没有什么实用价值,但是如果在不知道字典对象的键值时,这个办法就会变得很有用!
如果在树结构中,有时会用到递归,在每一层有个计算结果,我希望把这个结果保存起来,再遍历这一层时要用到这个结果时,这个静态的字典对象就可以帮上忙。
下面的代码是flex中实现
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
contentCreationComplete="windowedapplication1_contentCreationCompleteHandler(event)"
>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
public static var dic:Dictionary = new Dictionary();
public static var n:int = 0;
protected function button1_clickHandler(num:int):void
{
dic[num] ++;
trace("数值1",dic[num]);
}
protected function button2_clickHandler(num:int):void
{
dic[num] ++;
trace("数值2",dic[num]);
}
protected function windowedapplication1_contentCreationCompleteHandler(event:FlexEvent):void
{
dic[1] = 0;
dic[2] = 0;
}
]]>
</fx:Script>
<mx:Button label="数值1" click="button1_clickHandler(1)" x="50" y="100"/>
<mx:Button label=" 数值2" click="button2_clickHandler(2)" x="100" y="100"/>
</s:WindowedApplication>