重载操作符

重 载 操 作 符 重载操作符

1.索引器

索引器语法一般用于容器类的API,没有特别的好处,就是形式上简洁一些。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp基础语法
{
    class MyArray
    {
        private int[] items;

        public MyArray(int capacity)
        {
            items = new int[capacity];
        }
        public void SetItem(int i ,int val)
        {
            items[i] = val;
        }

        public int GetItem(int i)
        {
            return items[i];
        }

        // 索引器
        public int this[int index]
        {
            get
            {
                return items[index];
            }
            set
            {
                items[index] = value;
            }

        }

        public int Size()
        {
            return this.items.Length;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp基础语法
{
    class Program
    {
        static void Main(string[] args)
        {
            MyArray a = new MyArray(4);

            a[0] = 12;
            a[1] = 12;
            a[2] = 12;
            a[3] = 12;

            for(int i = 0; i < a.Size(); i++)
            {
                Console.WriteLine(a[i] + "  ");
            }

            a.SetItem(0, 16);
            a.SetItem(0, 16);
            a.SetItem(0, 16);
            a.SetItem(0, 16);
            for (int i = 0; i < a.Size(); i++)
            {
                Console.WriteLine(a.GetItem(i) + "  ");
            }
            Console.WriteLine("\n");
        }
    }
}


在这里插入图片描述

2.重载操作符

MyFraction f1 =new MyFraction(1, 3);
MyFraction f2 = new MyFraction(2, 5);
MyFraction result = f1 *f2;
Console.WriteLine("结果: "+result);

MyFraction是一个类,也能支持乘法运算


在C#里面,可以重载操作符,

public static MyFraction operator *(MyFraction a, MyFraction b){
	MyFraction result = new MyFraction();
	result.num = a.num " b.num;
	result.den = a.den * b.den;
	return result;
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp基础语法
{
    class MyFraction
    {
        public int num = 0;
        public int den = 1;

        public MyFraction() { }

        public MyFraction(int num ,int den)
        {
            this.num = num;
            this.den = den;
        }

        public override string ToString()
        {
            return num + "/" + den;
        }

        // 重载乘法操作符
        public static MyFraction operator *(MyFraction a,MyFraction b)
        {
            MyFraction res = new MyFraction();
            res.num = a.num * b.num;
            res.den = a.den * b.den;
            return res;
        }

        // 普通写法
        public static MyFraction  Multiply(MyFraction a, MyFraction b)
        {
            MyFraction res = new MyFraction();
            res.num = a.num * b.num;
            res.den = a.den * b.den;
            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp基础语法
{
    class Program
    {
        static void Main(string[] args)
        {
            // 定义两个分数 1/3 和 2/5
            MyFraction f1 = new MyFraction(1, 3);
            MyFraction f2 = new MyFraction(2, 5);

            // 相乘
            MyFraction res = f1 * f2;
            Console.WriteLine("结果: " + res);

        }
    }
}


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值