前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity3d+c#:Dictionary多个key查找一个value,重载Equals

Unity3d+c#:Dictionary多个key查找一个value,重载Equals

作者头像
立羽
发布2023-08-24 15:26:51
2060
发布2023-08-24 15:26:51
举报
文章被收录于专栏:Unity3d程序开发

Equals与GetHashCode

System.Object声明方法Equals和GetHashCode以及其他成员。 (注意:这个案例在C#中很重要)。您创建的类型会自动继承这些方法。

Equals的任务是将一个对象与另一个对象进行比较。引用类型的默认实现是比较引用。如果你想改变这种行为,你将不得不重写这个方法。

GetHashCode计算对象的哈希码并用于哈希表。例如,类型Dictionary<TKey,TValue>和HashSet利用它。 请参阅Hashtable and Dictionary Collection Types。如果您覆盖Equals,则必须覆盖GetHashCode以保持一致性。

DataKey1

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DictionaryEx
{
    public class DataKey1
    {
        public int mKey1;

        public DataKey1(int key)
        {
            mKey1 = key;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            DataKey1 key = obj as DataKey1;
            if (key == null) return false;

            return this.mKey1 == key.mKey1;
        }

        public override int GetHashCode()
        {
            return mKey1.GetHashCode();
        }
    }

    public class DataKey2 : DataKey1
    {
        public int mKey2;

        public DataKey2(int key1, int key2)
            : base(key1)
        {
            mKey2 = key2;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            DataKey2 key = obj as DataKey2;
            if (key == null) return false;

            return this.mKey1 == key.mKey1 && this.mKey2 == key.mKey2;
        }

        public override int GetHashCode()
        {

            return base.GetHashCode() ^ mKey2.GetHashCode();
        }
    }

    public class DataKey3 : DataKey2
    {
        public int mKey3;

        public DataKey3(int key1, int key2, int key3)
            : base(key1, key2)
        {
            mKey3 = key3;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            DataKey3 key = obj as DataKey3;
            if (key == null) return false;

            return this.mKey3 == key.mKey3 && this.mKey2 == key.mKey2 && this.mKey1 == key.mKey1;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ mKey3.GetHashCode();
        }
    }

    public class DataKey4 : DataKey3
    {
        public int mKey4;

        public DataKey4(int key1, int key2, int key3, int key4)
            : base(key1, key2, key3)
        {
            mKey4 = key4;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            DataKey4 key = obj as DataKey4;
            if (key == null) return false;

            return this.mKey4 == key.mKey4 && this.mKey3 == key.mKey3 && this.mKey2 == key.mKey2 && this.mKey1 == key.mKey1;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ mKey4.GetHashCode();
        }
    }
}

重载 public override bool Equals(object obj) 使类的比较为比较里面的成员变量int值

TableData

代码语言:javascript
复制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DictionaryEx
{
    public class TableData<K, T> //: CountString
        where K : DataKey1
    {
        protected const int mcHeadSize = 140;
        protected string mTableName;
        protected string mChineseTableName = "";
        protected int mTimeStamp;
        protected int mCurSeq;
        protected Dictionary<K, T> mTableData = new Dictionary<K, T>(131);

        protected List<T> mTableDataSequence = new List<T>(128);		

        public TableData(string tableName, string strChineseTableName)
        {
            mTableName = tableName;
            mChineseTableName = strChineseTableName;//上线后屏蔽
        }

        public TableData()
        {
            
        }

        public string name
        {
            get
            {
                return mTableName;
            }
        }

        public int timeStamp
        {
            set
            {
                mTimeStamp = value;
            }
            get
            {
                return mTimeStamp;
            }
        }

        public int curSeq
        {
            set
            {
                mCurSeq = value;
            }
            get
            {
                return mCurSeq;
            }
        }

        // 
        public List<T> GetTableSequenceData()
        {
            return mTableDataSequence;
        }

        public Dictionary<K, T> GetTableKeyValueData()
        {
            return mTableData;
        }

        public Int32 Count()
        {
            return mTableData.Count;
        }

        public T Find(K key)
        {
            T ret;

            if (mTableData.TryGetValue(key, out ret)) return ret;
            return default(T); //此关键字对于引用类型会返回空,对于数值类型会返回零。对于结构,此关键字将返回初始化为零或空的每个结构成员
        }

        public void Add(K key,T data)
        {
            mTableDataSequence.Add(data);
            mTableData.Add(key, data);
        }

        public void Remove(K key)
        {
            T data;
            if (mTableData.TryGetValue(key, out data))
            {
                mTableDataSequence.Remove(data);
                mTableData.Remove(key);
            }
        }
    }
}

使用与测试

代码语言:javascript
复制
TableData<DataKey2, int> dic = new TableData<DataKey2, int>();
            dic.Add(new DataKey2(1, 2), 1);
            dic.Add(new DataKey2(3,4), 2);

            Debug.Log(dic.Find(new DataKey2(1, 2)));

源码

https://github.com/luoyikun/UnityForTest DictionaryExScene场景

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Equals与GetHashCode
  • DataKey1
  • TableData
  • 使用与测试
  • 源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档