void main() {
Rectangle a = Rectangle(10, 10);
Rectangle b = Rectangle(5, 5);
Rectangle c = Rectangle(10, 10);
print(a == b); //false
print(a == c); //true
Rectangle d = a + b;
print('${d.width}--${d.height}'); //15--15
}
class Rectangle {
int width;
int height;
Rectangle(this.width, this.height);
@override
bool operator ==(dynamic other) {
if (other is! Rectangle) {
return false;
}
Rectangle temp = other;
return (temp.width == width && temp.height == height);
}
@override
Rectangle operator +(dynamic other) {
if (other is! Rectangle) {
return this;
}
Rectangle temp = other;
return Rectangle(temp.width + width, temp.height + height);
}
}
dart笔记6:重写操作符
dart笔记6:重写操作符:
