字典使用严格的 equals(===) 进行键比较,如何更改比较,这样我就可以使用我的标准进行比较,例如我有一个名为 Student 的类:
class Student{
var id:int;
var name:String;
var age:int;
//constructor
Student(id:int,name:String,age:int){
this.id = id;
this.name = name;
this.age = age;
}
}
我希望字典使用 id 来比较两个键是否相等,而不是使用 strict equal(===) 来比较键是否相同。
最佳答案
字典没有这样的功能。但除非我弄错了,否则你会从充满由 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 );
有什么理由不这样做吗?
关于flash.utils.Dictionary机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3581124/