zoukankan      html  css  js  c++  java
  • 定义一个底层的泛型

    public abstract class Entity<T> where T:Entity<T>
        {
            public Guid ID { get; private set; }
            public override bool Equals(object obj)
            {
                //第一个
                var other = obj as T;
                if (other == null) return false;
                //第二个
                var thisIsNew = Equals(ID, Guid.Empty);
                var otherIsNew = Equals(other.ID, Guid.Empty);
                if (thisIsNew && otherIsNew)
                    return ReferenceEquals(this, other);
                //第三个
                return ID.Equals(other.ID);
            }
            private int? oldHashCode;
            public override int GetHashCode()
            {
                // once we have a hashcode we'll never change it
                if (oldHashCode.HasValue)
                    return oldHashCode.Value;
                // when this instance is new we use the base hash code
                // and remember it, so an instance can NEVER change its
                // hash code.
                var thisIsNew = Equals(ID, Guid.Empty);
                if (thisIsNew)
                {
                    oldHashCode = base.GetHashCode();
                    return oldHashCode.Value;
                }
                return ID.GetHashCode();
            }
    
            public static bool operator ==(Entity<T> lhs, Entity<T> rhs)
            {
                return Equals(lhs, rhs);
            }
            public static bool operator !=(Entity<T> lhs, Entity<T> rhs)
            {
                return !Equals(lhs, rhs);
            }
        }
  • 相关阅读:
    GCD HDU
    Finding Lines UVALive
    Chinese Mahjong UVA
    DNA Evolution CodeForces
    String Reconstruction (并查集)
    Number Cutting Game HDU
    Paint the Wall ZOJ
    Star sky CodeForces
    Haunted Graveyard ZOJ
    GuGuFishtion HDU
  • 原文地址:https://www.cnblogs.com/lzhp/p/3105673.html
Copyright © 2011-2022 走看看