C#(十七)之C#重载

C#中的重载:包含两个方面,函数重载,运算符重载

(1):函数重载

最简单的例子就是Console.writeLine即可以打印字符串变量,又能打印整型变量。

class yunsuan {
    // 下边的两个方法除了返回值类型不同之外,其余都相同。
    // 这被称为C#的重载。
   public static int chu(int a,int b) {
        return a / b;
    }
   public static double chu(double a, double b)
    {
        return a / b;
    }
}

(2):构造函数重载

说的实在是高大上,其实就是参数不同

    public int age;
    public string name;
    /*构造函数重载*/
    public yunsuan() {
    
    }
    public yunsuan(int valueAge) {
        age = valueAge;
    }
    public yunsuan(string nameValue,int ageValue) {
        name = nameValue;
        age = ageValue;
    }

(3):运算符重载(重点)

说白了就是重写运算符的功能。

重载运算符由 operator 声明,必须是静态。

class tiji {
            public double length;
            public double width;
            public double height;
  
            public double getVolume()
            {
                return length * width * height;
            }
  
            // 重载 + 运算符来把两个 Box 对象相加
            public static tiji operator +(tiji b, tiji c)
            {
                tiji box = new tiji();
                box.length = b.length + c.length;
                box.width = b.width + c.width;
                box.height = b.height + c.height;
                return box;
            }
  
            public static tiji operator -(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
  
            public static tiji operator *(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
  
            public static tiji operator /(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
        }

测试使用代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*C#重载*/
            int a, b;
            double c, d;
            a = 25;
            b = 12;
            c = 10;
            d = 4;
            int f = yunsuan.chu(a,b);  // 输出2
            //Console.WriteLine(f);
            double ff = yunsuan.chu(c,d);  // 输出2.5
            //Console.WriteLine(ff);
            string qq = "My Name is ";
            qq = yunsuan.chu(qq);
            //Console.WriteLine(qq);  // 输出My Name Is gc
            yunsuan sd_one = new yunsuan();
            //Console.WriteLine("我叫{0},今年{1}岁", sd_one.name, sd_one.age);  // 我叫,今年0岁
            yunsuan sd_two = new yunsuan(23);
            //Console.WriteLine("我叫{0},今年{1}岁", sd_two.name, sd_two.age); // 我叫,今年23岁
            yunsuan sd_three = new yunsuan("gc",23);
            //Console.WriteLine("我叫{0},今年{1}岁", sd_three.name, sd_three.age); // 我叫gc,今年23岁
            tiji box1 = new tiji();
            tiji box2 = new tiji();
            tiji box3 = new tiji();
            double vol = 0;
            double vols = 0;
            box1.length = 12;
            box1.height = 13;
            box1.width = 14;
            vol = box1.getVolume();
            Console.WriteLine("box1体积是{0}",vol);
            box2.length = 15;
            box2.height = 16;
            box2.width = 17;
            vol = box2.getVolume();
            Console.WriteLine("box2体积是{0}", vol);
            box3 = box2 + box1;
            vols = box3.getVolume();
            Console.WriteLine("box2 + box1的体积是{0}", vols);
        }
        class tiji {
            public double length;
            public double width;
            public double height;
            public double getVolume()
            {
                return length * width * height;
            }
            // 重载 + 运算符来把两个 Box 对象相加
            public static tiji operator +(tiji b, tiji c)
            {
                tiji box = new tiji();
                box.length = b.length + c.length;
                box.width = b.width + c.width;
                box.height = b.height + c.height;
                return box;
            }
            public static tiji operator -(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
            public static tiji operator *(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
            public static tiji operator /(tiji a, tiji b)
            {
                tiji box = new tiji();
                box.length = a.length - b.length;
                box.width = a.width - b.width;
                box.height = a.height - b.height;
                return box;
            }
        }
        class yunsuan {
            public int age;
            public string name;
            /*构造函数重载*/
            public yunsuan() {
            
            }
            public yunsuan(int valueAge) {
                age = valueAge;
            }
            public yunsuan(string nameValue,int ageValue) {
                name = nameValue;
                age = ageValue;
            }
            // 下边的两个方法除了返回值类型不同之外,其余都相同。
            // 这被称为C#方法的重载。
            static public int chu(int a,int b) {
                return a / b;
            }
            static public double chu(double a, double b)
            {
                return a / b;
            }
            static public string chu(string b)
            {
                return b + "gc";
            }
        }
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

你可能感兴趣的:(C#,自学笔记,后端,重载,C#,构造函数,运算符)