您的位置:首页 > 编程语言 > C#

C# 运算符重载

2015-06-05 13:38 316 查看
  C#最常见的重载是构造函数重载,各种方法包括ToString()也可以重载,运算符+-*/也可以重载,今天我们就来说说运算符重载。

一、简介

  C# 允许用户定义的类型通过使用 operator 关键字定义静态成员函数来重载运算符。注意必须用public修饰且必须是类的静态的方法。但并非所有内置运算符都可以被重载,详见表1:

运算符可重载性
+、-、!、~、++、--、true、false可以重载这些一元运算符, true和false运算符必须成对重载
+、-、*、/、%、&、|、^、<<、>>可以重载这些二元运算符
==、!=、<、>、<=、>=可以重载比较运算符,必须成对重载
&&、||不能重载条件逻辑运算符,但可以使用能够重载的&和|进行计算
[]不能重载数组索引运算符,但可以定义索引器
()不能重载转换运算符,但可以定义新的转换运算符(请参见 explicit 和 implicit)
+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=不能显式重载赋值运算符,在重写单个运算符如+、-、%时,它们会被 隐式重写
=、.、?:、->、new、is、sizeof、typeof不能重载这些运算符
表1

二、声明

  operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:

public static result-type operator unary-operator ( op-type operand )

public static result-type operator binary-operator ( op-type operand, op-type2 operand2 )

public static implicit operator conv-type-out ( conv-type-in operand )

public static explicit operator conv-type-out ( conv-type-in operand )

  参数说明:

  result-type:运算符的结果类型。
  unary-operator:下列运算符之一:+ - ! ~ ++ — true false
  op-type:第一个(或唯一一个)参数的类型。
  operand:第一个(或唯一一个)参数的名称。
  binary-operator:其中一个:+ - * / % & | ^ << >> == != > < >= <=
  op-type2:第二个参数的类型。
  operand2:第二个参数的名称。
  conv-type-out:类型转换运算符的目标类型。
  conv-type-in:类型转换运算符的输入类型。

  注意:

  1、运算符重载的声明方式:operator 关键字告诉编译器,它实际上是一个运算符重载,后面是相关运算符的符号。

  2、运算符只能采用值参数,不能采用ref或out参数。可参考注意事项一实例。

  3、前两种形式声明了用户定义的重载内置运算符的运算符。op-type 和 op-type2 中至少有一个必须是封闭类型(即运算符所属的类型,或理解为自定义的类型)。例如,这将防止重定义整数加法运算符。可参考注意事项二实例。

  4、后两种形式声明了转换运算符。conv-type-in 和 conv-type-out 中正好有一个必须是封闭类型(即转换运算符只能从它的封闭类型转换为其他某个类型,或从其他某个类型转换为它的封闭类型)。

  5、对于二元运算符,第一个参数是放在运算符左边的值,一般命名为lhs;第二个参数是放在运算符右边的值,一般命名为rhs。

  6、C#要求所有的运算符重载都声明为publicstatic,必须是类的静态方法,这表示它们与它们的类或结构相关联,而不是与实例相关联

public class UserController : Controller
{
public ActionResult Index()
{
string message = string.Empty;
Teacher teaA = new Teacher(11, 30, "刘主任", "教导室主任");
Teacher teaB = new Teacher(12, 45, "吕老师", "校长助理");
Teacher teaC = new Teacher(11, 27, "刘老师", "小二班班主任");
Student stuOne = new Student(1, 18, "晓菜鸟");
Student stuTwo = new Student(2, 21, "博客园");
Student stuThree = new Student(1, 23, "攻城狮");
message = stuOne.Name + (stuOne == stuThree ? "是" : "不是") + stuThree.Name + "<br />";
message += stuOne.Name + (stuOne != stuTwo ? "不是" : "是") + stuTwo.Name + "<br />";
message += string.Format("{0}和{1}的年龄总和为:{2}<br />", stuOne.Name, stuThree.Name, stuOne + stuThree);
message += string.Format("{0}和{1}的年龄差为:{2}<br />", stuOne.Name, stuTwo.Name, stuOne - stuTwo);
message += stuOne;
ViewData.Model = message;
return View();
}
}

public class Student:People
{
public Student()
{ }

public Student(int id,int age, string name)
{
this.Id = id;
this.Age = age;
this.Name = name;
}

//重载ToString(),自定义格式化输出.
public override string ToString()
{
return "编号:" + Id + ";姓名:" + Name + ";年龄:" + Age;
}
}

public class Teacher:People
{
/// <summary> 职务 </summary>
public string Duties { get; set; }

public Teacher() { }

public Teacher(int id, int age, string name, string duties)
{
this.Id = id;
this.Age = age;
this.Name = name;
this.Duties = duties;
}
}

//abstract:抽象类用做基类,不能被实例化,用途是派生出其他非抽象类.
public abstract class People
{
public int Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }

//重载运算符"+",计算年龄总和.
public static int operator +(People lhs, People rhs)
{
return lhs.Age + rhs.Age;
}

//重载运算符"-",计算年龄差.
public static int operator -(People lhs, People rhs)
{
return Math.Abs(lhs.Age - rhs.Age);
}

//重载==运算符,Id相同则视为相等.
public static bool operator ==(People lhs, People rhs)
{
return lhs.Id == rhs.Id;
}

//比较运算符必须成对重载.
public static bool operator !=(People lhs, People rhs)
{
return !(lhs == rhs);
}
}


运算符重载实例三

"运算符重载实例三"运行结果图:



  关于运算符重载的内容就先介绍到这里,下一篇博客可能会写关于类型转换重载方面的问题,加油,晓菜鸟
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: