开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 4566|回复: 4
收起左侧

[C#作业] 第十天作业

[复制链接]
发表于 2019-4-13 15:29:12 | 显示全部楼层 |阅读模式   广东省揭阳市

第十天学习内容

10.1 密封类与部分类 IL简介 逆向程序集IL代码证实心中所想
10.2 超级基类Object源码阅读  Equals GetHashCode 以及 operator运算符重写
10.3 超级基类Object 重写ToString方法 Console类源码阅读

今日作业:

  1. 请设计一个 猫类 , 在猫对象的比对中我们认为 一个猫的品种相同 且 毛色 体重 相同就是同一只猫,请重写Object类中的 所有虚方法,并完成 == 与 != 运算符的重载,完成猫对象的比对
发表于 2019-5-31 00:17:05 | 显示全部楼层   湖南省长沙市

第十天超级基类重写

C#类库开发示例及在项目中该类库的方法
C#重写Equals()
C#重写ToString
C#用户空间的Dispose方法重写
重写 Finalize 方法

using System;

namespace 第十天超级基类重写
{
    class Program
    {
        static void Main(string[] args)
        {
            var c1 = new Cat { Species = "品种_橘猫", Coatcolor = "毛色_黄色", Weight = 2 };
            var c2 = new Cat { Species = "品种_橘猫", Coatcolor = "毛色_黄色", Weight = 2 };
            Console.WriteLine($"同一只:{c1.Equals(c2)}");
            if (c1 != c2) Console.WriteLine("不是同一只");
            else Console.WriteLine("同一只: Ture");
            Console.WriteLine(c1);
            Console.Read();
        }
    }

    class Cat : Object
    {
        public string Species { get; set; }
        public string Coatcolor { get; set; }
        public int Weight { get; set; }

        public override string ToString()
        {
            return $"这只猫的品种是:{this.Species},毛色是:{this.Coatcolor},体重是:{this.Weight}kg。";
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (this.GetType() != obj.GetType()) return false;
            return Equals((Cat)obj);
        }

        public bool Equals(Cat o)
        {
            if (o == null) return false;
            if (this.GetHashCode() != o.GetHashCode()) return false;
            return true ;
        }

        public override int GetHashCode()
        {
            return this.Species.GetHashCode() + this.Coatcolor.GetHashCode() + this.Weight.GetHashCode();
        }

        public static bool operator !=(Cat C1, Cat C2)
        {
            if (C1 is null) return false;
            if (ReferenceEquals(C2, null)) return false;
            if (C1.GetHashCode() != C2.GetHashCode()) return true;
            return false;
        }
        public static bool operator ==(Cat C1, Cat C2)
        {
            if (ReferenceEquals(C1, null)) return false;
            if (ReferenceEquals(C2, null)) return false;
            if (C1.GetHashCode() == C2.GetHashCode()) return true;
            return false;
        }

    }
}

评分

参与人数 1好评 +1 精币 +5 收起 理由
老郭 + 1 + 5 很赞同,谢谢!

查看全部评分

回复 支持 反对

使用道具 举报

结帖率:100% (1/1)
发表于 2019-4-29 19:06:39 | 显示全部楼层   江苏省南京市
本帖最后由 曜石头 于 2019-4-29 19:08 编辑

class Program
    {
           static void Main(string[] args)
         {
            var c1 = new Cat { Species = "品种1", Coatcolor = "毛色1", Weight = 2 };
            var c2 = new Cat { Species = "品种1", Coatcolor = "毛色1", Weight = 2 };
            Console.WriteLine($"这两只猫实际上是同一只:{c1.Equals(c2)}");
            if(c1 != c2) Console.WriteLine("这两只猫实际上不是同一只");                  
            else  Console.WriteLine("这两只猫实际上是同一只");
            Console.WriteLine(c1);
            Console.Read();
          }
    }


class Cat : Object {
        public string Species { get; set; }
        public string Coatcolor { get; set; }
        public int Weight { get; set; }

        public override string ToString()
        {
            return $"这只猫的品种是:{this.Species},毛色是:{this.Coatcolor},体重是:{this.Weight}kg。";
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (this.GetType() != obj.GetType()) return false;//判断类型是否相同,类型不相同就直接假就行
            return Equals((Cat)obj);
        }

        public bool Equals(Cat o) {
            if (o == null) return false;
           if (this.GetHashCode() != o.GetHashCode()) return false;//这里判断哈希值,哈希值相等一般字符串就相等
            return ture;
        }

        public override int GetHashCode()
        {
            return this.Species .GetHashCode ()+ this.Coatcolor.GetHashCode()+this.Weight.GetHashCode();
        }

        public static bool operator !=(Cat C1, Cat C2)
        {
            if (ReferenceEquals(C1, null)) return false;
            if (ReferenceEquals(C2, null)) return false;
            if (C1.GetHashCode() != C2.GetHashCode()) return true;
            return false;
        }

    }


评分

参与人数 1好评 +1 精币 +2 收起 理由
老郭 + 1 + 2 很赞同,谢谢!

查看全部评分

回复 支持 反对

使用道具 举报

发表于 2019-4-14 15:15:34 | 显示全部楼层   湖南省邵阳市
课上这句代码 ,return 应该是 true 吧。。。
if (this.GetType() == obj.GetType()) {
             return false;
            }


class Program
    {
           static void Main(string[] args)
         {
            var c1 = new Cat { Species = "品种1", Coatcolor = "毛色1", Weight = 2 };
            var c2 = new Cat { Species = "品种1", Coatcolor = "毛色1", Weight = 2 };
            Console.WriteLine($"这两只猫实际上是同一只:{c1.Equals(c2)}");
            Console.WriteLine($"这两只猫实际上是同一只:{c1 == c2}");
            Console.WriteLine(c1);
            Console.Read();
          }
    }


class Cat : Object {
        public string Species { get; set; }
        public string Coatcolor { get; set; }
        public int Weight { get; set; }

        public override string ToString()
        {
            return $"这只猫的品种是:{this.Species},毛色是:{this.Coatcolor},体重是:{this.Weight}kg。";
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (this.GetType() == obj.GetType()) return true;
            return Equals((Cat)obj);
        }

        public bool Equals(Cat o) {
            if (o == null) return false;
            if(this.GetHashCode()==o.GetHashCode()) return true;
            return false;
        }

        public override int GetHashCode()
        {
            return this.Species .GetHashCode ()+ this.Coatcolor.GetHashCode()+this.Weight.GetHashCode();
        }

        public static bool operator ==(Cat C1, Cat C2) {
            if (ReferenceEquals(C1, null)) return false;
            if (ReferenceEquals(C2, null)) return false;
            if(C1.GetHashCode ()== C2.GetHashCode()) return true;
            return false;
        }
        public static bool operator !=(Cat C1, Cat C2)
        {
            if (ReferenceEquals(C1, null)) return false;
            if (ReferenceEquals(C2, null)) return false;
            if (C1.GetHashCode() != C2.GetHashCode()) return true;
            return false;
        }

    }

点评

this.GetType() == obj.GetType() 应该是 不等于比对吧...   广东省揭阳市  发表于 2019-4-15 08:47

评分

参与人数 1精币 +1 收起 理由
老郭 + 1 很赞同,谢谢!

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

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