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); } }
部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!