相关疑难解决方法(0)

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

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

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

class Action {
  final Function function;
  final String description;

  Action(this.function, this.description);

  call() => function();

  toString() => description;
}
Run Code Online (Sandbox Code Playgroud)

现在我认为以下集合将包含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"));
}
Run Code Online (Sandbox Code Playgroud)

但相反,这个集合包含3个Action对象.看演示.如何确保在集合中看到相等的对象相等?

dart

5
推荐指数
1
解决办法
1067
查看次数

标签 统计

dart ×1