运算符重载,重载的一种类别(运算符重载)

使用运算符处理简单的类型变量(int、float、double,string等等),但是也可以把运算符用作类的对象。
运算符重载,重载的一种类别(运算符重载)运算符重载,重载的一种类别(运算符重载)运算符处理简单的类型 int intA = 4; int intB = 5; if (intA > intB) { //Code } else { //Code }

运算符用作类的对象:前提我们可以假设已经定义了运算符重载
运算符用作类的对象 public Class Student { private int score; public int Score { get{return score;} set{score=value;} } } public Class Test { static void Main(string[] args) { Student stu1 = new Student(); Student stu2 =new Student(); if(stu1>stu2) { //Code } } }

按照我们所刚刚所举的例子,来实现比较两个Student对象的成绩的大小:
View Code 1 class Student 2 { 3 private double score; 4 public double Score 5 { 6 set { score = value; } 7 get { return score; } 8 } 9 private string name; 10 public string Name 11 { 12 get { return name; } 13 set { name = value; } 14 } 15 public Student() 16 { 17 } 18 public Student(string name,double socre) 19 { 20 this.name = name; 21 this.Score = socre; 22 } 23 public static bool operator >(Student student1, Student student2) 24 { 25 return student1.Score>student2.Score; 26 } 27 public static bool operator <(Student student1, Student student2) 28 { 29 return student1.Score < student2.Score; 30 } 31 32 } 33 class Program 34 { 35 static void Main(string[] args) 36 { 37 Student stu1 = new Student("小王",43.4); 38 Student stu2 = new Student("小李",33.4); 39 if (stu1 > stu2) 40 { 41 Console.WriteLine( stu1.Name + "的成绩大于" + stu2.Name); 42 } 43 else 44 { 45 Console.WriteLine( stu1.Name + "的成绩小于" + stu2.Name); 46 } 47 } 48 }

可以推断出,运算符重载的基本格式如下:
public static 返回类型 operator 运算符(类名 形参1,........);并且可以知道如果我们重载了“>”运算符,相应的我们必须重载“<”运算符,反之亦然。
与此类似的运算符还有"=="与"!=",但是此时我们还要重写Equals(),GetHashCode()方法;运算符“>=”与“<=”等等;
举例:
按照上面例子,增加如下要求,求学生zs与学生ls的成绩之和:
View Code 1 class Student 2 { 3 private double score; 4 public double Score 5 { 6 set { score = value; } 7 get { return score; } 8 } 9 private string name; 10 public string Name 11 { 12 get { return name; } 13 set { name = value; } 14 } 15 public Student() 16 { 17 } 18 public Student(string name,double socre) 19 { 20 this.name = name; 21 this.Score = socre; 22 } 23 public static Student operator +(Student student, Student student2) 24 { 25 Student s = new Student(); 26 s.Score = student.score + student2.score; 27 return s; 28 } 29 } 30 class Program 31 { 32 static void Main(string[] args) 33 { 34 Student stu1 = new Student("小王",43.4); 35 Student stu2 = new Student("小李",33.4); 36 Student s = stu1 + stu2; 37 Console.WriteLine(s.Score); 38 } 39 }

比较两个学生的成绩是否相等:
View Code 1 class Student 2 { 3 private double score; 4 public double Score 5 { 6 set { score = value; } 7 get { return score; } 8 } 9 private string name; 10 public string Name 11 { 12 get { return name; } 13 set { name = value; } 14 } 15 public Student() 16 { 17 } 18 public Student(string name,double socre) 19 { 20 this.name = name; 21 this.Score = socre; 22 } 23 public static bool operator >(Student student1, Student student2) 24 { 25 return student1.Score>student2.Score; 26 } 27 public static bool operator <(Student student1, Student student2) 28 { 29 return student1.Score < student2.Score; 30 } 31 public static bool operator ==(Student student1, Student student2) 32 { 33 return student1.Score == student2.Score; 34 } 35 public static bool operator !=(Student student1, Student student2) 36 { 37 return student1.Score != student2.Score; 38 } 39 public override bool Equals(object obj) 40 { 41 if (obj is Student) 42 { 43 return this.score == ((Student)obj).Score; 44 } 45 else 46 { 47 throw new Exception("数据类型错误"); 48 } 49 } 50 public override int GetHashCode() 51 { 52 return (int)Math.Ceiling(this.score); 53 } 54 55 56 } 57 class Program 58 { 59 static void Main(string[] args) 60 { 61 Student stu1 = new Student("小王",43.4); 62 Student stu2 = new Student("小李",33.4); 63 if (stu1 == stu2) 64 { 65 Console.WriteLine(stu1.Name + "成绩等于" + stu2.Name); 66 } 67 else 68 { 69 Console.WriteLine(stu1.Name + "成绩不等于" + stu2.Name); 70 } 71 } 72 }

定义长度为4的Student类型的数组,然后求总成绩:
View Code 1 class Student 2 { 3 private double score; 4 public double Score 5 { 6 set { score = value; } 7 get { return score; } 8 } 9 private string name; 10 public string Name 11 { 12 get { return name; } 13 set { name = value; } 14 } 15 public Student() 16 { 17 } 18 public Student(string name,double socre) 19 { 20 this.name = name; 21 this.Score = socre; 22 } 23 24 25 public static Student operator +(Student student, Student student2) 26 { 27 Student s = new Student(); 28 s.Score = student.score + student2.score; 29 return s; 30 } 31 public static double operator +(double sum, Student student) 32 { 33 Student stu= new Student(); 34 stu.score = sum + student.Score; 35 return stu.Score; 36 } 37 } 38 class Program 39 { 40 static void Main(string[] args) 41 { 42 Student[] stu = new Student[4]; 43 stu[0] = new Student("zs", 34.4); 44 stu[1] = new Student("ls", 23.5); 45 stu[2] = new Student("ww", 34.5); 46 stu[3] = new Student("ml",23.1); 47 double sum = 0.0; 48 for (int i = 0; i < stu.Length; i++) 49 { 50 sum = sum +stu[i]; 51 } 52 Console.WriteLine(sum); 53 } 54 }

Tags:  矩阵运算符重载 重载下标运算符 等号运算符重载 重载赋值运算符 运算符重载

延伸阅读

最新评论

发表评论