两个对象==为啥不判断引用?
void main(){ var person1 = new person(20); var person2 = new person(20); print(person1 == person2);//这里的结果是true,视频里面老师也说了对象引用不一样,这两个对象,为啥==不判断引用? } class Person{ int age; Person(this.age); }
void main(){ var person1 = new person(20); var person2 = new person(20); print(person1 == person2);//这里的结果是true,视频里面老师也说了对象引用不一样,这两个对象,为啥==不判断引用? } class Person{ int age; Person(this.age); }
2019-11-24
如果你这样写返回值肯定是 false
你写的代码不全,对象进行操作符对比需要进行操作符覆写。。
void main(){ var person =new Student(20); var person1=new Student(20); print(person==person1); } class Student{ int age; Student(this.age); @override bool operator ==(Object other) => identical(this, other) || other is Student && age == other.age; @override int get hashCode => age.hashCode; }
举报