C# 运算符重载

 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 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;
        }

        //编译错误:二元运算符的参数之一必须是包含类型(参数c1、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);
        }   
    }

参考实例

三、实例

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 + "<br />";
            message += stuA.Name + (stuA != stuB ? "不是" : "是") + stuB.Name + "<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;
        }

        //重载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);
        }
    }

运算符重载实例三

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

image.png

 关于运算符重载的内容就先介绍到这里,下一篇博客可能会写关于类型转换重载方面的问题,加油,晓菜鸟



本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864