zoukankan      html  css  js  c++  java
  • 重载"=="和"!="运算符

    高手一看即知我要说什么,呵呵,请多批评

    假设一自定义类名TemplateEntity,来重载它的==和!=,C#语法规定如果重载了==则必须重载!=,反之亦然

    那,这个好实现,反正实体成员都是系统类型,可以直接使用其==或!=符号实现,想来不难~go~,分分种搞定的东东~

    代码如下:

    声明:此为错误的实现!且不可Copy之~
    /// <summary>
            
    /// 重载运算符==
            
    /// </summary>
            
    /// <param name="t1">TemplateEntity实例1</param>
            
    /// <param name="t2">TemplateEntity实例2</param>
            
    /// <returns></returns>
            public static bool operator ==( TemplateEntity e1, TemplateEntity e2 ) {
                
    if( e1 == null || e2 == null ) return false;
                
    if( e1 is TemplateEntity && e2 is TemplateEntity ) {
                    
    if( e1.Content == e2.Content
                        
    //&& e1.ID == e2.ID
                        && e1.Description == e2.Description
                        
    && e1.Extension == e2.Extension
                        
    && e1.Include == e2.Include
                        
    && e1.IsOnce == e2.IsOnce
                        
    && e1.Name == e2.Name
                        
    && e1.Path == e2.Path ) {
                        
    return false;
                    }
                }
                
    return true;
            }

    呵呵,问题出来了,一运行,报栈溢出~
    跟踪代码后发现不能在该方法内再次调用==和!=(PS:我们正在实现它,此时调用产生递归调用),这好办,不让使用==与null比较就使用Equals呗!
    改为e1.Equals(e2),运行,报错~未将对象引用设置到实例~呵呵,原因是e1不能为null,那也不难呀,再判断下e1不为null就行了呗~
    写上if( e1.Equals( null ) ) { return false; } ,null==null也要返回true的,问题又来了,什么问题嗫?
    就是e1,e2同时为null的时候,无法比较了!因为调用null.Equals会报未将对象设置引用到实例的错误~又转回去了!
    再后来发生的事情略过,越写越乱,一怒之下弃之~

    最后参考MSDN代码实现如下
    此方法为参考MSDN修改而来
    public static bool operator ==( TemplateEntity a, TemplateEntity b ) {
                
    // If both are null, or both are same instance, return true.
                if( System.Object.ReferenceEquals( a, b ) ) {
                    
    return true;
                }

                
    // If one is null, but not both, return false.
                if( ( (object)a == null ) || ( (object)b == null ) ) {
                    
    return false;
                }

                
    // Return true if the fields match:
                return a.Cont ent == b.Content
                    
    && a.Description == b.Description
                    
    && a.Extension == b.Extension
                    
    && a.Include == b.Include
                    
    && a.IsOnce == b.IsOnce
                    
    && a.Name == b.Name
                    
    //&& a.ID == b.ID
                    && a.Path == b.Path;
            }
    上述代码是可以正常运行的,
    写完==后马上着手写!=的实现,
    代码如下
    演示错误或不好的写法
    public static bool operator !=( TemplateEntity a, TemplateEntity b ) {
                
    // If both are null, or both are same instance, return true.
                if( System.Object.ReferenceEquals( a, b ) ) {
                    
    return false;
                }

                
    // If one is null, but not both, return false.
                if( ( (object)a == null ) || ( (object)b == null ) ) {
                    
    return true;
                }

                
    // Return true if the fields match:
                return a.Content != b.Content
                    
    && a.Description != b.Description
                    
    && a.Extension != b.Extension
                    
    && a.Include != b.Include
                    
    && a.IsOnce != b.IsOnce
                    
    && a.Name != b.Name
                    
    //&& a.ID != b.ID
                    && a.Path != b.Path;
            }

    呵呵,大家是不是都看出来了,实现了==后,调用!=直接取反就可以了,所以可以简写如下
    正确的或比较完美的实现
    ///  <summary>
            
    /// 重载运算符!=
            
    /// </summary>
            
    /// <param name="t1">TemplateEntity实例1</param>
            
    /// <param name="t2">TemplateEntity实例2</param>
            
    /// <returns></returns>
            public static bool operator !=( TemplateEntity a, TemplateEntity b ) {
                
    return !( a == b );
            }
    乱七八糟的写了一通,希望大家不会看晕,实在组织不好伦次了,就这么着吧~总结下~
    有些问题在动手时才会发现,就像重载操作符一样,
    脑子里一闪过而的想法就是:不难实现!
    判断类型是否相等,判断成员是否相等,结合返回比较结果即可,
    其实还应该考虑值相等,引用相等,类型相等等等问题,引用相等的情况下一定是值相等,值相等的情况下不一定是引用相等,这些都比较简单,贴上MSDN地址,感兴趣的朋友自己看看吧,
    继续
    说完了吗?不.大家一定看到方法中的最后一行了,比较各个成员值是否相同,也是我重载操作符的初衷,然而,写完上面的代码之后,我才发现,
    其实第一印象是正确的!那就是应该继承IComparable接口,或者重写Equals方法,这才是值比较!而==多数情况下是引用比较~
    那好了,就当写作业了~希望大家能把文章看完,不要被我举的错误的例子误导~因为我就经常把老师举的错误的例子记住,反而把正确的做法认为是不安全的,不可靠的从而导致做错,呵呵,在此提个醒~


    ------------------------------------------
    除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
    BUY ME COFFEE
  • 相关阅读:
    jquery 代码搜集
    Windows Server 2008中安装IIS7.0
    javascript 判断两个日期之间的天数 兼容ie,firefox
    jquery选择器大全
    原始ajax方式调用asp.net后台方法
    JavaScript及C# URI编码详解
    利用JQuery直接调用asp.net后台方法
    C#操作XML小结_转载
    从bnbt tracker源码分析bt客户端与traker的通信
    传说中的神器: shared_ptr/weak_ptr/scoped_ptr
  • 原文地址:https://www.cnblogs.com/kkun/p/1383817.html
Copyright © 2011-2022 走看看