搜集的运算符重载。。。。。。。。。。。。。。。。。。。。。。。。。。。编程
using System; using System.Collections.Generic; using System.Linq; using System.Text; //简单的运算符重载 方便理解 namespace 运算符重载 { public class Money { public decimal Weight { get; set; } public static decimal operator -(Money a1, Money a2)//public static 必需要写上。operator是关键字 { return a1.Weight - a2.Weight; } } class Program { static void Main(string[] args) { Money a = new Money { Weight = 100.00M }; Money b = new Money { Weight = 200.66M }; Console.Write(b - a); Console.Read(); } } }
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、ide
C#最多见的重载是构造函数重载,各类方法包括ToString()也能够重载,运算符+-*/也能够重载,今天咱们就来讲说运算符重载。函数
1、简介
C# 容许用户定义的类型经过使用 operator 关键字定义静态成员函数来重载运算符。注意必须用public修饰且必须是类的静态的方法。但并不是全部内置运算符均可以被重载,详见表1:
this
2、声明
operator 关键字用于在类或结构声明中声明运算符。运算符声明能够采用下列四种形式之一:spa
public static result-type operator unary-operator ( op-type operand )code
public static result-type operator binary-operator ( op-type operand, op-type2 operand2 )orm
public static implicit operator conv-type-out ( conv-type-in operand )对象
public static explicit operator conv-type-out ( conv-type-in operand )blog
参数说明:继承
result-type:运算符的结果类型。
unary-operator:下列运算符之一:+ - ! ~ ++ — true false
op-type:第一个(或惟一一个)参数的类型。
operand:第一个(或惟一一个)参数的名称。
binary-operator:其中一个:+ - * / % & | ^ << >> == != > < >= <=
op-type2:第二个参数的类型。
operand2:第二个参数的名称。
conv-type-out:类型转换运算符的目标类型。
conv-type-in:类型转换运算符的输入类型。
注意:
一、运算符重载的声明方式:operator 关键字告诉编译器,它其实是一个运算符重载,后面是相关运算符的符号。
二、运算符只能采用值参数,不能采用ref或out参数。可参考注意事项一实例。
三、前两种形式声明了用户定义的重载内置运算符的运算符。op-type 和 op-type2 中至少有一个必须是封闭类型(即运算符所属的类型,或理解为自定义的类型)。例如,这将防止重定义整数加法运算符。可参考注意事项二实例。
四、后两种形式声明了转换运算符。conv-type-in 和 conv-type-out 中正好有一个必须是封闭类型(即转换运算符只能从它的封闭类型转换为其余某个类型,或从其余某个类型转换为它的封闭类型)。
五、对于二元运算符,第一个参数是放在运算符左边的值,通常命名为lhs;第二个参数是放在运算符右边的值,通常命名为rhs。
六、C#要求全部的运算符重载都声明为public和static,必须是类的静态方法,这表示它们与它们的类或结构相关联,而不是与实例相关联。
注意事项一:
public class Student
{
public int Age { get; set; }
public string Name { get; set; }
public Student() { } public Student(int age, string name) { this.Age = age; this.Name = name; } //语法错误:ref和out参数在此上下文中无效(去掉ref和out关键字便可). public static Student operator +(ref Student stu1,out Student stu2) { return new Student(stu1.Age + stu2.Age, stu1.Name + "+++" + stu2.Name); } }
注意事项二:
public class Student
{
public int Age { get; set; }
public string Name { get; set; }
public Student() { } public Student(int age, string name) { this.Age = age; this.Name = name; } //编译错误:二元运算符的参数之一必须是包含类型(参数c一、c2中有一个类型为Student便可). public static Student operator +(int c1, int c2) { return new Student(c1 + c2, "晓菜鸟"); } }
比较运算符的重载:
a、C#要求成对重载比较运算符,若是重载了==,也必须重载!=,不然会产生编译错误。
b、比较运算符必须返回bool类型的值,这是与其余算术运算符的根本区别。
c、在重载==和!=时,还应该重载从System.Object中继承的Equals()和GetHashCode()方法,不然会产生一个编译警告,缘由是Equals方法应执行与==运算符相同的相等逻辑。
d、C# 不容许重载=运算符,但若是重载例如+运算符,编译器会自动使用+运算符的重载来执行+=运算符的操做。
e、任何运算符声明的前面均可以有一个可选的属性(C# 编程指南)列表。
重点:
运算符重载其实就是函数重载。首先经过指定的运算表达式调用对应的运算符函数,而后再将运算对象转化为运算符函数的实参,接着根据实参的类型来肯定须要调用的函数的重载,这个过程是由编译器完成。
参考实例:
public class UserController : Controller
{
public ActionResult Index()
{
Student student = new Student(18, "博客园");
var resultOne = student + 3;
var resultTwo = student + "晓菜鸟";
return View();
}
}
public class Student { public int Age { get; set; } public string Name { get; set; } public Student() { } public Student(int age, string name) { this.Age = age; this.Name = name; } public static Student operator +(Student stu, int c2) { return new Student(stu.Age + c2, stu.Name + "-晓菜鸟"); } public static Student operator +(Student stu, string suffix) { return new Student(stu.Age + 11, stu.Name + suffix); } }
3、实例
运算符重载实例一:
public class UserController : Controller
{
public ActionResult Index()
{
string message = string.Empty;
Student stuA = new Student(1, 18, "晓菜鸟");
Student stuB = new Student(2, 21, "博客园");
Student stuC = new Student(1, 23, "攻城狮");
message = stuA.Name + (stuA == stuC ? "是" : "不是") + stuC.Name + "
";
message += stuA.Name + (stuA != stuB ? "不是" : "是") + stuB.Name + "
";
message += stuA;
ViewData.Model = message;
return View();
}
}
public class Student { public int Id { get; set; } public int Age { get; set; } public string Name { get; set; } 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 { public int Id { get; set; } public string Name { get; set; } public int Duties { get; set; } //重载运算符"+",计算两个学生的年龄总和. public static Student operator +(Student lhs, Student rhs) { return new Student(0, lhs.Age + rhs.Age, lhs.Name + " 和 " + rhs.Name); } //重载运算符"-",计算两个学生的年龄差. public static Student operator -(Student lhs, Student rhs) { return new Student(0, Math.Abs(lhs.Age - rhs.Age), lhs.Name + " 和 " + rhs.Name); } //重载==运算符,同一Id的学生默认为同一我的. public static bool operator ==(Student lhs, Student rhs) { return lhs.Id == rhs.Id; } //比较运算符必须成对重载. public static bool operator !=(Student lhs, Student rhs) { return !(lhs == rhs); } }
编译"运算符重载实例一"将产生错误,错误信息:二元运算符的参数之一必须是包含类型。这里的错误跟咱们上面的"注意事项二实例"的错误大同小异,由于在Teacher类中,他不知道Student是什么,只有Student本身知道。只有Student才能决定本身能不能"+-",而不能让别人决定。operator + 至关于一个函数,咱们能够这样去理解,operator +(op-type operand, op-type2 operand2) 等于 op-type.operator +(operand,operand2) 或者 op-type2.operator +(operand,operand2)。
运算符重载实例二:
public ActionResult Index() { string message = string.Empty; Student stuA = new Student(1, 18, "晓菜鸟"); Student stuB = new Student(2, 21, "博客园"); Student stuC = new Student(1, 23, "攻城狮"); message = stuA.Name + (stuA == stuC ? "是" : "不是") + stuC.Name + "<br />"; message += stuA.Name + (stuA != stuB ? "不是" : "是") + stuB.Name + "<br />"; Student stuSum = stuA + stuC; Student stuDiffe = stuA - stuB; message += stuSum.Name + "的年龄总和为:" + stuSum.Age + "<br />"; message += stuDiffe.Name + "的年龄差为:" + stuDiffe.Age + "<br />"; message += stuA; ViewData.Model = message; return View(); } } public class Student { public int Id { get; set; } public int Age { get; set; } public string Name { get; set; } public Student() { } public Student(int id,int age, string name) { this.Id = id; this.Age = age; this.Name = name; } //重载运算符"+",计算两个学生的年龄总和. public static Student operator +(Student lhs, Student rhs) { return new Student(0, lhs.Age + rhs.Age, lhs.Name + " 和 " + rhs.Name); } //重载运算符"-",计算两个学生的年龄差. public static Student operator -(Student lhs, Student rhs) { return new Student(0, Math.Abs(lhs.Age - rhs.Age), lhs.Name + " 和 " + rhs.Name); } //重载==运算符,同一Id的学生默认为同一我的. public static bool operator ==(Student lhs, Student rhs) { return lhs.Id == rhs.Id; } //比较运算符必须成对重载. public static bool operator !=(Student lhs, Student rhs) { return !(lhs == rhs); } //重载ToString(),自定义格式化输出. public override string ToString() { return "编号:" + Id + ";姓名:" + Name + ";年龄:" + Age; } } public class Teacher { public int Id { get; set; } public string Name { get; set; } public int Duties { get; set; } }
"运算符重载实例二"是彻底没有问题的,这个时候咱们想一个问题,将如咱们的Teacher类也涉及到求教师年龄的总和和差值怎么办?难道只能重写一遍?不知道您有什么好的思路和看法,不妨在评论里面留下您的见解!请多多指教,晓菜鸟不胜感激!
我这里想到的就是继承,让子类去继承父类的重载!请看"运算符重载实例三"。
运算符重载实例三:
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); } }
"运算符重载实例三"运行结果图: