【问题标题】:What is the difference between 'is' and '==' in Dart?Dart中的'is'和'=='有什么区别?
【发布时间】:2020-08-16 14:19:29
【问题描述】:

假设我有:

class Test<T> {
  void method() {
    if (T is int) {
      // T is int
    } 

    if (T == int) {
      // T is int
    }
  }
}

我知道我可以覆盖 == 运算符,但是如果我不覆盖任何运算符,Dart 中 == 和 is 之间的主要区别是什么。


编辑:

说我有

extension MyIterable<T extends num> on Iterable<T> {
  T sum() {
    T total = T is int ? 0 : 0.0; // setting `T == int` works
    for (T item in this) {
      total += item;
    }
    return total;
  }
}

当我使用我的扩展方法时:

var addition = MyIterable([1, 2, 3]).sum();

我收到此错误:

type 'double' 不是 type 'int' 的子类型

【问题讨论】:

    标签: dart


    【解决方案1】:
    • identical(x, y) 检查x 是否与y 相同。

    • x == y 检查是否应将x 视为等于y。 default implementation for operator == 与 identical() 相同,但 operator == 可以被覆盖以进行深度相等检查(或者理论上可能是病态的并被实施以做任何事情)。

    • x is T 检查x 是否具有T 类型。 x 是一个对象实例。

    class MyClass {
      MyClass(this.x);
    
      int x;
    
      @override
      bool operator==(dynamic other) {
        return runtimeType == other.runtimeType && x == other.x;
      }
    
      @override
      int get hashCode => x.hashCode;
    }
    
    void main() {
      var c1 = MyClass(42);
      var c2 = MyClass(42);
      var sameC = c1;
    
      print(identical(c1, c2));    // Prints: false
      print(identical(c1, sameC)); // Prints: true
    
      print(c1 == c2);    // Prints: true
      print(c1 == sameC); // Prints: true
    
      print(c1 is MyClass);      // Prints: true
      print(c1 is c1);           // Illegal.  The right-hand-side must be a type.
      print(MyClass is MyClass); // Prints: false
    }
    

    注意最后一种情况:MyClass is MyClass 是 false,因为左侧是 type,而不是 MyClass 的 instance。 (不过,MyClass is Type 将是 true。)

    在您的代码中,T is int 不正确,因为双方都是类型。在这种情况下,您确实想要T == int。请注意,T == int 将检查 exact 类型,如果一个是另一个的派生类型,则不会为 true(例如,int == num 将为 false)。

    【讨论】:

    • 这可能是最好的答案,非常感谢您,先生,我希望我能更多地投票。
    【解决方案2】:

    ==基本上是相等运算符,"is"是Dart的instanceof运算符(如果你有Java背景,如果不是,它基本上会告诉你是否something 是类型 something)。

    当你想检查两个对象是否相等时,使用 == 表示相等。你可以在你的类中实现 == 操作符(方法)来定义你想根据什么判断两个对象是否相等。

    举个例子:

    class Car {
        String model;
        String brand;
        Car(this.model, this.brand);
    
        bool operator == (otherObj) {
            return (otherObj is Car && other.brand == brand); //or however you want to check
            //On the above line, we use "is" to check if otherObj is of type Car
        }
    }
    

    现在您可以根据您定义的条件检查两辆车是否“相等”。

    void main() {
      final Car micra = Car("Micra", "Nissan");
      print(micra == Car("Micra", "Nissan")); // true
      print(micra is Car("Micra", "Nissan")); // true
    }
    

    因此,== 是您用来决定两个对象是否相等的东西,您可以根据您对如何将两个对象视为相等的期望来覆盖和设置它。

    另一方面,“is”基本上告诉您实例是否属于 object 类型(这里 micra 属于 Car 类型)。

    【讨论】:

    • Dart 语言之旅有关于 equality operators 和 type 运算符的部分。
    • @jamesdlin 谢谢,这是否意味着如果两个对象相等,它们将永远相同?
    • @iKeepChangingName No. identical 表示完全相同的对象实例。两个单独的对象可以是==(具有相同的内部值)但不会是identical。
    • @ Yudhishthir Singh 感谢您的回答,我已经知道相同和相等的东西,我已经编辑了我的代码,您可以查看我是否使用 == 代码有效,但使用 is 失败,我期待相反的结果。你能解释一下为什么会出现错误吗?
    • @jamesdlin 非常感谢您,先生,您非常有帮助,我已经更新了我的代码,您能否告诉我它有什么问题,如果您能发布答案,我会很高兴。再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2019-08-19
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多