C#_运算符的重载.ppt

C#运算符的重载 运算符重载 将自定义的类型赋予运算符号,使得对象和对象可以进行运算,称为运算符重载。 如果有一自定义的类型命名为「二维座标」,若重载「+」运算符,则 (3, 5)+(4, -3)=(7, 2) c#里的运算符重载是个不常出现的应用,同时也是个好玩的东西。 operator 关键字用于在类或结构声明中声明运算符。: 运算符重载 // 重载一元运算符原型声明 public static return-type operator op(type operand) { // statement; } // 重载二元运算符原型声明 public static return-type operator op(type operand1,type operand2) { // statement; } class A { int x; public int X { get { return x; } set { x = value; } } //重载加操作符 public static A operator +(A a, A b) { A c = new A(); c.x = a.x + b.x; return c;

文档评论(0)

1亿VIP精品文档

相关文档