开发者

flash.utils.Dictionary mechanism

The dictionary use strict equals(===) for key comparison, how to change the comparison, so I can use my standard for comparison, for example, I have a class named Student:

    class Student{
     var id:int;
     var name:String;
     var age:int;
    //constructor
   Student(id:int,name:String,age:int){
         this.id开发者_Python百科 = id;
         this.name = name;
         this.age = age;
      }
    }

I want Dictionary use id to compare if the two keys are equal, not use strict equal(===) to compare if the key is the same.


Dictionary doesn't have any functionality like this. But unless I'm mistaken you'd get largely the same effect from a Dictionary (or Vector or Array or Object - whatever collection best suits your needs) full of Students indexed by id:

var studentsByID:Array = []; // <-- could be a vector, dict, custom collection class, etc..
// ...
studentsByID[someID] = new Student( someID, someName, someAge );
// ...
trace( "my ID:" + myID );
trace( "my Name:" + (studentsByID[myID] as Student).name );

Is there any reason not to do that?


you can do next:

class Student{
 var id:int;
 var name:String;
 var age:int;
Student(id:int,name:String,age:int){
     this.id = id;
     this.name = name;
     this.age = age;
  }
    public function equals(s:Student):Boolean{
        return ((this.id==s.id)&&(this.name==s.name)&&(this.age==s.age));
    }
}

so later use it like:

var d:Dictionary=new Dictionary();
d["key"]=new Student();
//snippet
(d["key"] as Student).equals(new Student());

also if you want to overload as3 operators read next things:

Overload [] operator in AS3 http://livedocs.adobe.com/flex/3/langref/flash/utils/Proxy.html

Best Regards Eugene

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜