c#中,运算符使用时,可以将运算符的方法进行重载
重载加号运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| class Box { private double length; private double width; private double height;
public override string ToString() { return this.length+","+ this.width+","+this.height; }
public double getVolume() { return length * width * height; } public void setLength(double length) { this.length = length; }
public void setBreadth(double width) { this.width = width; }
public void (double height) { this.height = height; }
public static Box operator +(Box b, Box c) { Box box = new Box(); box.length = 10; box.width = 10; box.height = 10; return box; } }
|
在类里面重写运算符的方法,当外面有两个实例对象进行运算符运输时,就会触发该方法
能被重载的运算符
