C# 运算符重载
- 运算符重载
- 运算符重载的实现
- 1. 类中重载的方法必须是public公有的
- 2. 类中定义的重载方法必须是静态的
- 3. 在运算符前需要加上关键字 operator
- 使用如下
- 可重载和不可重载运算符
- 实例
运算符重载
一提到+ - * / %这种类似的运算符都应该很清楚是什么,但是程序是怎么实现的呢?
程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。
运算符重载的实现
重载运算符注意几点:
1. 类中重载的方法必须是public公有的
2. 类中定义的重载方法必须是静态的
3. 在运算符前需要加上关键字 operator
class Box{private int length; // 长度private int breadth; // 宽度private int height; // 高度public int getVolume(){return length * breadth * height;}public void setLength(int len){length = len;}public void setBreadth(int bre){breadth = bre;}public void setHeight(int hei){height = hei;}public static Box operator + (Box b,Box c){Box box = new Box();box.height = b.height + c.height;box.length = b.length + c.length;box.breadth = b.breadth + c.breadth;return box;}public static Box operator -(Box b, Box c){Box box = new Box();box.height = b.height - c.height;box.length = b.length - c.length;box.breadth = b.breadth - c.breadth;return box;}public static Box operator *(Box b, Box c){Box box = new Box();box.height = b.height * c.height;box.length = b.length * c.length;box.breadth = b.breadth * c.breadth;return box;}public static Box operator /(Box b, Box c){Box box = new Box();box.height = b.height / c.height;box.length = b.length / c.length;box.breadth = b.breadth / c.breadth;return box;}}
使用如下
Box box1 = new Box();Box box2 = new Box();Box box3 = new Box();int volume = 0;//box1box1.setLength(6);box1.setBreadth(7);box1.setHeight(5);//box2box2.setLength(12);box2.setBreadth(13);box2.setHeight(10);volume = box1.getVolume();Debug.LogError(volume);volume = box2.getVolume();Debug.LogError(volume);box3 = box1 + box2;volume = box3.getVolume();Debug.LogError(volume);
当上面的代码被编译和执行时,它会产生下列结果:
可重载和不可重载运算符
运算符 | 描述 |
---|---|
+, -, !, ~, ++, – | 这些一元运算符只有一个操作数,且可以被重载。 |
+, -, *, /, % | 这些二元运算符只有一个操作数,且可以被重载。 |
==, !=, <, >, <=, >= | 这些比较运算符可以被重载。 |
&&,ll | 这些一元运算符只有一个操作数,且可以被重载。 |
+=, -=, *=, /=, %= | 这些赋值运算符不能被重载。 |
=, ., ?:, ->, new, is, sizeof, typeof | 这些运算符不能被重载。 |
实例
// 重载 + 运算符来把两个 Box 对象相加public static Box operator+ (Box b, Box c){Box box = new Box();box.length = b.length + c.length;box.breadth = b.breadth + c.breadth;box.height = b.height + c.height;return box;}public static bool operator == (Box lhs, Box rhs){bool status = false;if (lhs.length == rhs.length && lhs.height == rhs.height && lhs.breadth == rhs.breadth){status = true;}return status;}public static bool operator !=(Box lhs, Box rhs){bool status = false;if (lhs.length != rhs.length || lhs.height != rhs.height || lhs.breadth != rhs.breadth){status = true;}return status;}public static bool operator <(Box lhs, Box rhs){bool status = false;if (lhs.length < rhs.length && lhs.height < rhs.height && lhs.breadth < rhs.breadth){status = true;}return status;}public static bool operator >(Box lhs, Box rhs){bool status = false;if (lhs.length > rhs.length && lhs.height > rhs.height && lhs.breadth > rhs.breadth){status = true;}return status;}public static bool operator <=(Box lhs, Box rhs){bool status = false;if (lhs.length <= rhs.length && lhs.height <= rhs.height && lhs.breadth <= rhs.breadth){status = true;}return status;}public static bool operator >=(Box lhs, Box rhs){bool status = false;if (lhs.length >= rhs.length && lhs.height >= rhs.height && lhs.breadth >= rhs.breadth){status = true;}return status;}public override string ToString(){return String.Format("({0}, {1}, {2})", length, breadth, height);}