吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1024|回复: 1
收起左侧

[学习记录] C# 学习笔记 运算符重载

  [复制链接]
Cool_Breeze 发表于 2021-3-12 15:33
本帖最后由 Cool_Breeze 于 2021-3-12 16:19 编辑

[C#] 纯文本查看 复制代码
using System;

namespace Operator
{
    class Program
    {
        static void Main()
        {
            Box a = new Box(2);
            Box b = new Box(3);
            
            Console.WriteLine(a + b);  // 5 这里返回一个新的 Box 实例,但是被丢弃了
            Console.WriteLine(a >= b); // false
            Console.WriteLine(a <= b); // true
            Console.WriteLine(a++);    // 12 重载时 我们把 Count 的值加上了 10
            Console.WriteLine(a--);    // 7  重载时 我们把 Count 的值减去了 5
            Console.WriteLine(+a);     // 7
            Console.WriteLine(-a);     // -7
            Console.WriteLine(!a);     // False
            a.Count += 7;              // a.Count = 0
            Console.WriteLine(a);      // 0
            Console.WriteLine(!a);     // true
            Console.WriteLine(a++);    // 10
            Console.WriteLine(~a);
            // 取反前二进制码:
            // 00000000,00000000,00000000,00001010
            // 取反后二进制码:
            // 11111111,11111111,11111111,11110101
            // -11
        }
    }
    
    
    class Box
    {
        public int Count {get; set;}
        
        public Box(int v)
        {
            Count = v;
        }
        
        // 重写 ToString 方法 ToString 继承自 object
        // Console.WriteLine 会直接输出 Count 的值;
        public override string ToString()
        {
            return String.Format("{0}", Count);
        }
        
        // 二元操作符 + 需要两个参数
        // 两个相同对象相加,返回相同类型实例 static 修饰符后面的 Box 就是返回值 类型
        public static Box operator + (Box a, Box b)
        {
            Box c = new Box(0);
            c.Count = a.Count + b.Count;
            return c;
        }
        
        // >= 和 <= 必须成对出现,不然会报编译错误 返回值类型为 bool
        public static bool operator >= (Box a, Box b)
        {
            
            if (a.Count >= b.Count)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public static bool operator <= (Box a, Box b)
        {
            if (a.Count <= b.Count) return true;
            else return false;
        }
        
        
        
        // 一元操作符 + 只需要一个参数
        public static Box operator + (Box a)
        {
            a.Count = Math.Abs(a.Count);
            return a;
        }
        
        public static Box operator - (Box a)
        {
            a.Count = 0 - a.Count;
            return a;
        }
        
        // 定义 Count 的值 等于 0 或者 等于 null 时,Count 的值为 false 然后取反 为 true
        public static bool operator ! (Box a)
        {
            if (a.Count == 0 || (int?)a.Count == null) // 值为 false (int?)显示类型转换
            {
                return true; // 取反返回 true
            }
            else return false; 
        }
        
        // 自增运算符不需要创建一个实例,使用它自己
        public static Box operator ++ (Box a)
        {
            a.Count += 10;
            return a;
        }
        
        public static Box operator -- (Box a)
        {
            a.Count -= 5;
            return a;
        }
        
        // 按位取反
        public static Box operator ~ (Box a)
        {
            Console.WriteLine("取反前二进制码:");
            DisplayBinary(a.Count);
            a.Count = ~a.Count;
            Console.WriteLine("取反后二进制码:");
            DisplayBinary(a.Count);
            return a;
        }

        public static void DisplayBinary(int Number) // 显示二进制位
        {
            unsafe // 不安全代码 指针
            {
                int Count = Number; // int 4 个字节
                byte* p = (byte*)&Count;  // byte 1个字节
                for (sbyte i = 3; i >= 0; i--)
                {
                    for (sbyte j = 7; j >= 0; j--) // 一个字节等于 8 个比特位
                    {
                        if (((*(p + i) & (1<<j))) != 0)
                        {
                            Console.Write(1);
                        }
                        else
                        {
                            Console.Write(0);
                        }
                    }
                    if (i != 0) Console.Write(',');
                }
            }
            Console.Write('\n');
            
            // for (sbyte i = 31; i >= 0; i--) // 不使用指针版
            // {
                // if ((Number & (1<<i)) != 0)
                // {
                    // Console.Write(1);
                // }
                // else
                // {
                    // Console.Write(0);
                // }
            // }
        }

    }
}

免费评分

参与人数 2吾爱币 +3 热心值 +2 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
樱染落霞红 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Cool_Breeze 发表于 2021-3-12 19:26
C# 微软官方源代码查看地址
[C#] 纯文本查看 复制代码
https://referencesource.microsoft.com
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-17 22:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表