简体   繁体   English

dart是否支持运算符重载

[英]Does dart support operator overloading

I read that Dart does not support function overloading.我读到 Dart 不支持 function 重载。 Does it support operator overloading?它是否支持运算符重载? If yes, can you show me how it's done in a simple example?如果是的话,你能用一个简单的例子告诉我它是如何完成的吗? And what are some advantages etc?有哪些优点等?

Dart does support operator overloading using the operator keyword followed by the operator you want to overload. Dart 支持使用operator关键字后跟要重载的运算符的运算符重载。 The following example overloads the == operator for the MyClass object:以下示例重载了MyClass对象的==运算符:

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

almost all Darts built-in operators can be overloaded with a few notable exceptions being the assignment operator = and reference equivalence operator === (doesn't exist anymore).几乎所有 Darts 内置运算符都可以重载,但有一些值得注意的例外是赋值运算符= 和引用等价运算符 === (不再存在)。

As for the advantage of operator overloading, it allows you to reuse operators that have a well known semantic meaning such as == or + for operations on your objects.至于运算符重载的优势,它允许您重用具有众所周知的语义含义的运算符,例如==+对您的对象进行操作。 For example, if you have a Matrix class that overloads the + operator then you can add two matrices using the syntax m1 + m2 instead of the more cumbersome m1.plus(m2)例如,如果您有一个重载+运算符的 Matrix 类,那么您可以使用语法m1 + m2而不是更麻烦的m1.plus(m2)来添加两个矩阵

The chosen answer is no longer valid when you try overloads using the == operator in the new version.在新版本中尝试使用==运算符重载时,所选答案不再有效。 Now you need to do this:现在你需要这样做:

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

But it's not safe.但这并不安全。 other is not specified as a type, Something unexpected may happened. other未指定为类型,可能会发生意外。 For example:例如:

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;
}

So you could do like this:所以你可以这样做:

class A {
  A(this.index);

  final int index;

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

You need to use covariant .您需要使用covariant Because Object overloads the == operator.因为 Object 重载了==运算符。

or you can或者你可以

Test Object Type:测试对象类型:
visit: hash_and_equals访问: 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;
}

To extend Lars' answer, you can also overload operators using the inline function syntax.为了扩展 Lars 的答案,您还可以使用内联函数语法重载运算符。

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

A amazing example to learn how to use operator overloading is a class to handle complex numbers in dart :学习如何使用运算符重载的一个很好的例子是在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})';
  }
}

Since Dart version 2.7 you can add operators to existing classes, for example:从 Dart 2.7 版开始,您可以向现有类添加运算符,例如:

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

Using the Complex numbers as sample, I can implement "Complex * num" like:使用复数作为示例,我可以实现“Complex * num”,例如:

Complex operator *(dynamic b) {
  if (b is Complex) {
    return Complex( real: ... );
  } else if (b is num) {
    return Complex.ri(real*b, imaginary*b);
  }
}
...

But how implement "num * Complex"?但是如何实现“num * Complex”呢? Is possible?有可能吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM