我爱模板网 > 建站教程 > dart >  dart笔记6:重写操作符正文

dart笔记6:重写操作符

dart笔记6:重写操作符:
01void main() {
02  Rectangle a = Rectangle(10, 10);
03  Rectangle b = Rectangle(5, 5);
04  Rectangle c = Rectangle(10, 10);
05  print(a == b); //false
06  print(a == c); //true
07 
08  Rectangle d = a + b;
09  print('${d.width}--${d.height}'); //15--15
10}
11 
12class Rectangle {
13  int width;
14  int height;
15 
16  Rectangle(this.width, this.height);
17 
18  @override
19  bool operator ==(dynamic other) {
20    if (other is! Rectangle) {
21      return false;
22    }
23    Rectangle temp = other;
24    return (temp.width == width && temp.height == height);
25  }
26 
27  @override
28  Rectangle operator +(dynamic other) {
29    if (other is! Rectangle) {
30      return this;
31    }
32    Rectangle temp = other;
33    return Rectangle(temp.width + width, temp.height + height);
34  }
35}



部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!
上一篇:dart笔记5:重构override+画圆 下一篇:dart笔记7:dart abstract抽象类
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢