# 飞镖是否支持运算符重载？ （不要与覆盖混淆）

##### 问题描述投票：0回答：4

``````int max(int a, int b);
double max(double a, double b);
``````

docs表示存在可重写的运算符。因此，我看到您可以实现自定义运算符。同时，dart不支持重载方法。那么，dart是否支持重载运算符？

``````class Matrix{
Matrix operator+(int b){//...};
Matrix operator+(Matrix b({//...};
}
``````
dart
##### 4个回答
3

``````class Matrix {
int num = 0;
Matrix(this.num);
Matrix operator+(dynamic b) {
if(b is int) {
return Matrix(this.num + b);
} else if(b is Matrix){
return Matrix(this.num + b.num);
}
}
}

void main() {
print((Matrix(5) + 6).num);

print((Matrix(7) + Matrix(3)).num);
}
``````

0

``````class A{
operator*(int b){
print("mul int");
}
operator*(double b){
print("mul double");
}
}
``````

``````Error compiling to JavaScript:
main.dart:5:11:
Error: '*' is already declared in this scope.
operator*(double b){
``````

0

`geometry.dart`中有以下几行：

``````  /// Unary negation operator.
///
/// Returns an offset with the coordinates negated.
///
/// If the [Offset] represents an arrow on a plane, this operator returns the
/// same arrow but pointing in the reverse direction.
Offset operator -() => Offset(-dx, -dy);

After some research it appears that `-` operator can be used with 0 or 1 parameter, which allows it to be defined twice.

/// Binary subtraction operator.
///
/// Returns an offset whose [dx] value is the left-hand-side operand's [dx]
/// minus the right-hand-side operand's [dx] and whose [dy] value is the
/// left-hand-side operand's [dy] minus the right-hand-side operand's [dy].
///
Offset operator -(Offset other) => Offset(dx - other.dx, dy - other.dy);

``````

0

Dart语言规范说：

## 10.1.1运算符

Dart不支持重载方法（或函数），运算符等效于方法，因此，ergo，Dart不支持运算符重载。