我看到Dart不支持函数重载。它是否支持运算符重载?如果支持,能否给我展示一个简单的例子?另外,有哪些优点等相关信息呢?
我看到Dart不支持函数重载。它是否支持运算符重载?如果支持,能否给我展示一个简单的例子?另外,有哪些优点等相关信息呢?
在新版本中使用过载==
运算符进行重载时,所选择的答案不再有效。现在需要执行以下操作:
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
修饰符所示。相比之下,重载(overloading)允许在一个类中多次定义相同的方法或运算符,每次使用不同的签名(即参数列表)。Dart 不支持此功能,但可以使用 dynamic
或命名参数进行模拟。 - BerDart支持使用operator关键字和要重载的运算符来进行运算符重载。下面的示例重载了MyClass对象的==运算符:
class MyClass {
operator ==(MyClass other) {
// compare this to other
}
}
几乎所有Dart内置运算符都可以进行重载,但有一些值得注意的例外,其中包括赋值运算符=和引用等价运算符 === (已经不存在)。
至于运算符重载的优点,它允许您重用具有良好语义意义的运算符,例如 == 或 + ,来执行对象的操作。例如,如果您有一个矩阵类,它重载了 + 运算符,则可以使用语法 m1 + m2 添加两个矩阵,而不是使用更麻烦的 m1.plus(m2)。
++
或--
运算符? - Second Person Shooterclass MyClass {
operator ==(MyClass o) => id == o.id;
}
一个很好的例子来学习如何使用运算符重载是用于处理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://dev59.com/J53ha4cB1Zd3GeqPTFos#41146661
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})';
}
}
extension Contains on String {
bool operator <<(Pattern other) => contains(other);
bool operator >>(String other) => other.contains(this);
}
以复数为例,我可以实现“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);
}
}
...
但是如何实现“num * Complex”?这是可能的吗?