关于比较对象
判断两个引用是否指向同一个对象,直接使用"=="就可以,但是有时需要用到比较两个对象的值,即每个字段的值是否相同就不能这么比较可以使用ObjectUtil.compare(a:Object, b:Object, depth:int = -1):int函数来进行比较。
package { public class Student { public function Student(_name:String, _sex:String) { this.name = _name; this.sex = _sex; } public var name:String; public var sex:String; } }
测试:
//import mx.utils.ObjectUtil;
var s0:Student = new Student("Bill", "M"); var s1:Student = s0; var s2:Student = new Student("Bill", "M"); var s3:Student = new Student("Tom", "M"); trace(s0==s1); //true trace(s0==s2); //false trace(s0==s3); //false trace(ObjectUtil.compare(s0,s2)==0);//true trace(ObjectUtil.compare(s0,s3)==0);//false