如何重写Object的Equals方法?

来源:互联网 发布:blog搭建并与mysql分离 编辑:程序博客网 时间:2024/06/05 15:58
重写模式分为三类:
1。为基类没有重写Object.Equals方法的引用类型实现Equals
2。为基类重写Object.Equals方法的引用类型实现Equals
3。为值类型实现Equals

具体实现代码如下:(其中为了节省篇幅,没有重写GetHashCode方法)

using System;
//引用类型
public class RefType
{
}
//值类型
public struct ValType
{
}
public struct MyValType
{
    private RefType r1;
    private ValType v1;
    public override Boolean Equals(object obj)
    {
        if(!(obj is MyValType)) return false;
        //调用强类型的Equals方法,其实应当尽量不要调用这个方法,
        //因为如果MyValType和别的类型定义了隐式转换的话,这
        //个方法返回true,但是实际上两者并不是同一个类型!!!
        return this.Equals((MyValType)obj);
       
    }
   
    public Boolean Equals(MyValType o)
    {
        //比较引用字段
        if(!object.Equals(this.r1,o.r1)) return false;
        //比较值字段
        if(!this.v1.Equals(o.v1)) return false;
       
        return true;
       
    }
    //强类型的操作符重载
    public static Boolean operator==(MyValType o1,MyValType o2)
    {
        return object.Equals(o1,o2);
    }
    public static Boolean operator!=(MyValType o1,MyValType o2)
    {
        return !(o1==o2);
    }
}
//自定义引用类
public class BaseRefType:object
{
    private RefType r1;
    private ValType v1;
    //override object类型中的Equals方法
    public override Boolean Equals(Object obj)
    {
        if(obj==null) return false;
        if(this.GetType()!=obj.GetType()) return false;
       
        BaseRefType t=(BaseRefType)obj;
        //这里使用Object.Equals这个静态方法,因为这个方法保证了
        //即使两个字段都为null,代码仍然会执行下去(可以查看其
        //源代码,其中有判断是否为null的保护)
        //
        if(!Object.Equals(this.r1,t.r1)) return false;
        //这里就不使用Object.Equals这个静态方法了,因为值类型永远
        //不会为null,而且Object.Equals这个静态方法还会执行装箱操作
        if(!this.v1.Equals(t.v1)) return false;
       
        return true;
    }
    public static Boolean operator==(BaseRefType o1,BaseRefType o2)
    {
        return object.Equals(o1,o2);

    }
    public static Boolean operator!=(BaseRefType o1,BaseRefType o2)
    {
        return !(o1==o2);
    }
}
//自定义引用类的派生类类
public class MyRefType:BaseRefType
{
    private RefType r1;
    private ValType v1;
    public override Boolean Equals(Object obj)
    {
        //因为此自定义类的基类override了Object的Equals方法,所以
        //需要首先调用基类的Equals方法
        if(!base.Equals(obj)) return false;
        if(obj==null) return false;
        if(this.GetType()!=obj.GetType()) return false;
       
        MyRefType t=(MyRefType)obj;
        //比较引用字段
        if(!Object.Equals(this.r1,t.r1)) return false;
        //比较值字段
        if(!this.v1.Equals(t.v1)) return false;
       
        return true;
    }
    //重载==和!=运算符
    public static Boolean operator==(MyRefType o1,MyRefType o2)
    {
        return object.Equals(o1,o2);
    }
    public static Boolean operator!=(MyRefType o1,MyRefType o2)
    {
        return !(o1==o2);
    }
}

public class App
{
    static void Main(String[] args)
    {
        MyRefType o1=new MyRefType();
        MyRefType o2=new MyRefType();
        if(o1==o2)
            Console.WriteLine("o1==o2");
        else
            Console.WriteLine("o1!=o2");
        Console.ReadLine();
    }
   
   
}

原创粉丝点击