Flutter 什么';s使用按位异或运算符的意义^&引用;获取/生成对象的哈希代码?

Flutter 什么';s使用按位异或运算符的意义^&引用;获取/生成对象的哈希代码?,flutter,dart,object-comparison,Flutter,Dart,Object Comparison,在阅读一些代码时,我注意到一些开发人员使用按位异或运算符,^,来生成对象的哈希代码 这样做有什么意义?与其他方法相比,它在获取/生成对象的哈希代码方面是否有一些优势 下面是一个代码示例 class Student { final String name; final int age; Student(this.name, this.age); @override bool operator ==(other) { return (other is Student)

在阅读一些代码时,我注意到一些开发人员使用按位异或运算符,
^
,来生成对象的哈希代码

这样做有什么意义?与其他方法相比,它在获取/生成对象的哈希代码方面是否有一些优势

下面是一个代码示例

class Student {
  final String name;
  final int age;

  Student(this.name, this.age);

  @override
  bool operator ==(other) {
    return (other is Student) && other.name == name && other.age == age;
  }

  @override
  int get hashCode => age.hashCode ^ name.hashCode; // <-- Here
}
班级学生{
最后的字符串名;
最终积分;
学生(这个名字,这个年龄);
@凌驾
布尔运算符==(其他){
return(其他为学生)和&other.name==name和&other.age==age;
}
@凌驾

int get hashCode=>age.hashCode^name.hashCode;//它必须是一个数字,并且它在对象的任何成员的更多位上的变化越大越好。

你的意思是按位异或的结果必须是一个数字吗?为什么/在那种情况下,你会选择这个来生成hashCode?这很好地回答了这个问题: