精易论坛

标题: 第十天作业 [打印本页]

作者: 老郭    时间: 2019-4-13 15:29
标题: 第十天作业

第十天学习内容


10.1 密封类与部分类 IL简介 逆向程序集IL代码证实心中所想

10.2 超级基类Object源码阅读  Equals GetHashCode 以及 operator运算符重写

10.3 超级基类Object 重写ToString方法 Console类源码阅读



今日作业:



  1. 请设计一个 猫类 , 在猫对象的比对中我们认为 一个猫的品种相同 且 毛色 体重 相同就是同一只猫,请重写Object类中的 所有虚方法,并完成 == 与 != 运算符的重载,完成猫对象的比对



作者: qingshanlushui    时间: 2019-4-14 15:15
课上这句代码 ,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;
        }

    }
作者: 曜石头    时间: 2019-4-29 19:06
本帖最后由 曜石头 于 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;
        }

    }



作者: 鱼塘是我的    时间: 2019-5-31 00:17


第十天超级基类重写



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;
        }

    }
}






欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4