【问题标题】:Does dart support operator overloadingdart 是否支持运算符重载
【发布时间】:2012-04-25 04:56:55
【问题描述】:

我读到 Dart 不支持函数重载。它是否支持运算符重载?如果是的话,你能用一个简单的例子告诉我它是如何完成的吗?还有哪些优势等?

【问题讨论】:

  • 您从哪里了解到Dart 不支持函数重载

标签: function overloading operator-keyword dart


【解决方案1】:

当您在新版本中使用== 运算符尝试重载时,所选答案不再有效。现在你需要这样做:

class MyClass {
  @override
  bool operator ==(other) {
    // compare this to other
  }
}

但这并不安全。 other 未指定为类型,可能会发生意外情况。例如:

void main() {
  var a = A(1);
  var b = B(1);
  var result = a == b;
  print(result); //result is true
}

class A {
  A(this.index);

  final int index;

  @override
  bool operator ==(other) => other.index == index;
}

class B {
  B(this.index);

  final int index;
}

所以你可以这样做:

class A {
  A(this.index);

  final int index;

  @override
  bool operator ==(covariant A other) => other.index == index;
}

您需要使用covariant。因为 Object 重载了== 运算符。

或者你可以

测试对象类型:
访问:hash_and_equals

class A {
  A(this.index);

  final int index;

  @override
  bool operator ==(other) => other is A && (other.index == index);

  @override
  int get hashCode => index;
}

【讨论】:

  • 实际上我们在这里讨论的是覆盖,正如@override 装饰器所暗示的那样。相比之下,重载将允许在一个类中多次定义相同的方法或运算符,每次都有不同的签名(即参数列表)。 Dart 不支持此功能,但可以使用 dynamic 或命名参数进行模拟。
【解决方案2】:

Dart 确实支持运算符重载,使用 operator 关键字后跟要重载的运算符。以下示例重载了 MyClass 对象的 == 运算符:

class MyClass {
  operator ==(MyClass other) {
    // compare this to other
  }
}

几乎所有 Darts 内置运算符都可以重载,但有一些值得注意的例外是赋值运算符 = 和引用等价运算符 ===(不再存在)。

至于运算符重载的优势,它允许您重用具有众所周知的语义含义的运算符,例如 ==+ 来对您的对象进行操作。例如,如果您有一个重载 + 运算符的 Matrix 类,那么您可以使用语法 m1 + m2 来添加两个矩阵,而不是更繁琐的 m1。加(平方米)

【讨论】:

  • 可以告诉我“其他”是否有什么特别之处,或者是否可以将其命名为其他名称。喜欢 ==(MyClass ggg)
  • @MuhammadUmer other 可以命名为任何名称,它只是您要比较的类对象的参数名称,即 this == other 或 this + other
  • 参数是否必须是相同的类型,或者可以创建类似 Scala 的 List cons operator :: 的运算符,其中左侧的类型是创建新头部的元素类型,类型右边是 List 类型?
  • 关于覆盖 == 时需要注意的常见陷阱,请参阅 work.j832.com/2014/05/equality-and-dart.html
  • 如何在 Dart 中重载 ++--
【解决方案3】:

要扩展 Lars 的答案,您还可以使用内联函数语法重载运算符。

class MyClass {
  operator ==(MyClass o) => id == o.id;
}

【讨论】:

    【解决方案4】:

    学习如何使用运算符重载的一个惊人示例是在 dart 中处理复数的类:

    import 'dart:core';
    
    class Complex {
      final double real;
      final double imaginary;
    
      Complex({this.real = 0, this.imaginary = 0});
    
      Complex.ri(this.real, this.imaginary);
    
      Complex operator +(Complex b) {
        return Complex(
            real: this.real + b.real, imaginary: this.imaginary + b.imaginary);
      }
    
      Complex operator -(Complex b) {
        return Complex(
            real: this.real - b.real, imaginary: this.imaginary - b.imaginary);
      }
    
      Complex operator *(Complex b) {
        return Complex(
            real: this.real * b.real - this.imaginary * b.imaginary,
            imaginary: this.real * b.imaginary + this.imaginary * b.real);
      }
    
      Complex operator /(Complex b) {
        // https://stackoverflow.com/a/41146661/6846888
        var conjugation = b.conjugate();
        var denominatorRes = b * conjugation;
    
        // denominator has only real part
        var denominator = denominatorRes.real;
        var nominator = this * conjugation;
    
        return Complex(
            real: nominator.real / denominator,
            imaginary: nominator.imaginary / denominator);
      }
    
      bool operator ==(b) {
        return b.real == this.real && b.imaginary == this.imaginary;
      }
    
      @override
      String toString() {
        return 'Complex(real: ${real}, imaginary: ${imaginary})';
      }
    }
    

    【讨论】:

      【解决方案5】:

      从 Dart 2.7 版开始,您可以将运算符添加到现有类中,例如:

      extension Contains on String {
        bool operator <<(Pattern other) => contains(other);
        bool operator >>(String other) => other.contains(this);
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-11
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 2014-07-31
        • 2019-10-22
        • 2011-08-27
        • 1970-01-01
        • 2022-06-29
        相关资源
        最近更新 更多