C# 类是否相等

public class ShopItem {
    public string name;
    public int count;
    public int buyPrice;
    public int sellPrice;
    public float price;

    public ShopItem()
    {
    }

    public ShopItem(string _name, int _count, int _buyPrice, int _sellPrice, float _price)
    {
        // TODO: Complete member initialization
        this.name = _name;
        this.count = _count;
        this.buyPrice = _buyPrice;
        this.sellPrice = _sellPrice;
        this.price = _price;
    }

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

        ShopItem tempObj = (ShopItem)obj;

        return (name.Equals(tempObj.name))      &&
            (count == tempObj.count)            && 
            (buyPrice == tempObj.buyPrice)      && 
            (sellPrice == tempObj.sellPrice)    && 
            (Mathf.Abs(price - tempObj.price) < 0.0001);
    }
}

猜你喜欢

转载自blog.csdn.net/lihuozhiling/article/details/42963999