美文网首页
C# overload 重载

C# overload 重载

作者: 合肥黑 | 来源:发表于2022-01-26 08:26 被阅读0次

    C#重载(overload)小结(一切尽有,建议收藏!!!)

    重载分为三种:

    • 构造函数重载
    • 一般方法重载
    • 运算符重载
    1.构造函数重载
    class Student
    {
        public Student()
        {
            Console.WriteLine("无参构造函数");
        }
        public Student(string name, int code)
        {
            Console.WriteLine("具有两个参数的构造函数");
        }
        public Student(int code,string name)
        {
            //这里就说明了上面所讲到的,以两个方法为例,参数类型相同
            //(但是需要在每个方法中的参数类型不同前提下),参数个数相同,参数顺序不同。
            Console.WriteLine("也具有两个参数的构造函数,但是和上面的参数顺序不一样");
        }
        public Student(int code, string name, string sex, int num)
        {
            Console.WriteLine("具有四个参数的构造函数");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("===========重载=============");
            Student stu1 = new Student();
            Student stu2 = new Student("小明", 85);
            Student stu3 = new Student(85,"小明");
            Student stu4 = new Student(99, "张三", "男", 2019);
            Console.ReadLine();
        }
    }
    
    2.一般方法重载
    class Student
    { 
        public void run()
        {
            Console.WriteLine("无参run方法");
        }
        public void run(int code, int num)
        {
            Console.WriteLine("具有两个整型参数的run方法");
        }
        public void run(int code, string name)
        {
            Console.WriteLine("具有两个参数(先整型后字符串)的run方法");
        }
        //这里也说明了参数类型相同,参数个数相同,参数顺序不同
        public void run( string name, int code)
        {
            Console.WriteLine("具有两个参数(先字符串后整型)的run方法");
        }
        public void run(int num, string name, int code)
        {
            Console.WriteLine("具有三个参数的run方法");
        }
        public void run(string name, int code, string sex, int num)
        {
            Console.WriteLine("具有四个参数的run方法");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("===========重载=============");
            Student stu1 = new Student();
            stu1.run();
            stu1.run(88, 2020);
            stu1.run(92,"李四");
            stu1.run("李四",92);
            stu1.run(2018, "李四", 76);
            stu1.run("王麻子", 60, "男", 2017);
            Console.ReadLine();
        }
    }
    
    3.运算符重载

    https://www.cnblogs.com/MedlarCanFly/p/11409486.html
    所有一元和二元运算符都具有可自动用于任何表达式的预定义实现。除了预定义实现外,还可通过在类或结构中包括 operator 声明来引入用户定义的实现。
    可重载的一元运算符 (overloadable unary operator) 有:

    +   -   !   ~   ++   --   true   false
    

    可重载的二元运算符 (overloadable binary operator) 有:

    +   -   *   /   %   &   |   ^   <<   >>   ==   !=   >   <   >=   <=
    

    做了一个 学生+学生 return 新的学生 的案例

    class Program
        {
            static void Main(string[] args)
            {
                //实例化一个男孩
                Student boy = new Student() { Sex = true };
                //实例化一个女孩
                Student girl = new Student() { Sex = false };
                Student student = boy + girl;
                Console.WriteLine($"哇! 是个{(student.Sex?"男":"女")}孩");
                Console.ReadKey();
            }
    
        }
        class Student {
            public bool Sex { get; set; }
            public static Student operator +(Student stu1, Student stu2)
            {
                //当Sex不同的时候相加才能放回一个Student
                if (stu1.Sex != stu2.Sex)
                {
                    Random random = new Random();
                    //返回一个随机性别的Student
                    return new Student() { Sex = (random.Next(2) == 0) };
                }
                return null;
            }
            public static Student operator !(Student student)
            {
                //转变Sex
                return new Student() { Sex=!student.Sex };
            }
        }
    

    相关文章

      网友评论

          本文标题:C# overload 重载

          本文链接:https://www.haomeiwen.com/subject/nishcrtx.html