问题
I read that Dart does not support function overloading. Does it support operator overloading. If yes, would be kind and show me how in a simple example how its done. And what are some advantages etc. I am new to programming. Thanks.
回答1:
The chosen answer is no longer valid when you try overloads the == operator in new version .Now you need do like 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.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 cloud do like this:
class A {
A(this.index);
final int index;
@override
bool operator ==(covariant A other) => other.index == index;
}
You need use covariant
.Because Object overloads the == operator.
回答2:
Yes Dart does support operator overloading using the operator keyword followed by the operator you want to overload. The following example overloads the == operator for the MyClass object:
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).
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)
回答3:
To extend Lars' answer, you can also overload operators using the inline function syntax.
class MyClass {
operator ==(MyClass o) => id == o.id;
}
回答4:
A amazing example to learn how to use operator overloading is a class to handle complex numbers in 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})';
}
}
来源:https://stackoverflow.com/questions/10130472/does-dart-support-operator-overloading