一组如何确定两个对象在dart中是否相等?

时间:2015-04-10 17:27:11

标签: dart

我不明白集合如何确定两个对象何时相等。更具体地说,一个集合的add方法何时真正添加一个新对象,何时它不是一个新对象,因为该对象已经在集合中?

例如,我有以下类中的对象:

class Action {
  final Function function;
  final String description;

  Action(this.function, this.description);

  call() => function();

  toString() => description;
}

现在我认为以下集合将包含2个元素,因为其中2个是相同的:

void main() {
  Set<Action> actions = new Set()
    ..add(new Action(() => print("a"), "print a"))  
    ..add(new Action(() => print("a"), "print a"))
    ..add(new Action(() => print("b"), "print b"));
}

但相反,此集包含3个Action个对象。请参阅demo。如何确保在集合中看到相等的对象相等?

2 个答案:

答案 0 :(得分:9)

有关Dart中operator==的全面说明,请参阅http://work.j832.com/2014/05/equality-and-dart.html

它只是检查它们是否相等a == b您可以覆盖==运算符来自定义此行为。请注意,覆盖hashCode运算符时,还应覆盖==

class Action {
  @override
  bool operator==(other) {
    // Dart ensures that operator== isn't called with null
    // if(other == null) {
    //   return false;
    // }
    if(other is! Action) {
      return false;
    }
    return description == (other as Action).description;
  }

  // hashCode must never change otherwise the value can't
  // be found anymore for example when used as key 
  // in hashMaps therefore we cache it after first creation.
  // If you want to combine more values in hashCode creation
  // see http://stackoverflow.com/a/26648915/217408
  // This is just one attempt, depending on your requirements
  // different handling might be more appropriate.
  // As far as I am aware there is no correct answer for
  // objects where the members taking part of hashCode and
  // equality calculation are mutable.
  // See also http://stackoverflow.com/a/27609/217408
  int _hashCode;
  @override
  int get hashCode {
    if(_hashCode == null) {
      _hashCode = description.hashCode
    }
    return _hashCode;
  }
  // when the key (description) is immutable and the only
  // member of the key you can just use
  // int get hashCode => description.hashCode
}

尝试DartPad

答案 1 :(得分:0)

以下是测试顶级函数、静态方法和实例方法是否相等的示例:

void foo() {} // A top-level function

class A {
  static void bar() {} // A static method
  void baz() {} // An instance method
}

void main() {
  var x;

  // Comparing top-level functions.
  x = foo;
  assert(foo == x);

  // Comparing static methods.
  x = A.bar;
  assert(A.bar == x);

  // Comparing instance methods.
  var v = A(); // Instance #1 of A
  var w = A(); // Instance #2 of A
  var y = w;
  x = w.baz;

  // These closures refer to the same instance (#2),
  // so they're equal.
  assert(y.baz == x);

  // These closures refer to different instances,
  // so they're unequal.
  assert(v.baz != w.baz);
}