The ABC of Dart Part 5 - 操作符重载

Dart可以像C语言一样重载部分操作符。支持重载的操作符有:

<+^[]
>/&[]=
<=~/<<~
>=*>>==
%

重载通过operator关键字实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Point{
int x;
int y;

Point(this.x, this.y);

//重载加号运算符
Point operator +(Point point){
return new Point(x + point.x, y + point.y);
}
}

void main(){
Point p1 = new Point(1,2);
Point p2 = new Point(3,4);
print((p1 + p2).x);//打印4
print((p1 + p2).y);//打印6
}
The ABC of Dart Part 6 - 库类的引用 The ABC of Dart Part 4 - 集合

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×