通过枚举值实现赋值、取值、触发时间
来源:互联网 发布:超市收银软件排名 编辑:程序博客网 时间:2024/04/29 21:13
public class PropertySet<N, T> where N : struct ,IConvertible //基类 where T : IComparable{ protected T[] mValues; protected Action<T>[] mActions; public PropertySet(N maxCount) { int count = ToIndex(maxCount); mValues = new T[count]; mActions = new Action<T>[count]; } public void AddChanageEvent(N key, Action<T> handler) { mActions[ToIndex(key)] += handler; } public void RemoveChangeEvent(N key, Action<T> handler) { mActions[ToIndex(key)] -= handler; } public virtual int ToIndex(N key) { return key.ToInt32(null); //12bytes GC Alloc } public virtual T this[N key] { get { return mValues[ToIndex(key)]; } set { int index = ToIndex(key); if (value.CompareTo(mValues[index]) != 0) { mValues[index] = value; if (mActions[index] != null) { mActions[index](value); } } } }}public class PlayerPropertySet : PropertySet<EPlayerProperty, float>{ public PlayerPropertySet() : base(EPlayerProperty.Max){mCryptoValues = new FloatCryptoWrapper[(int)EPlayerProperty.Max];for (int i = 0; i < mCryptoValues.Length; i++){mCryptoValues[i] = new FloatCryptoWrapper(0.0f);}} public override int ToIndex(EPlayerProperty key) { return (int)key; } public override float this[EPlayerProperty key] { get { return mCryptoValues[ToIndex(key)].Value; } set { int index = ToIndex(key);if ((mCryptoValues[ToIndex(key)].Value) != value) {mCryptoValues[ToIndex(key)].Value = value; if (mActions[index] != null) { mActions[index](value);//触发时间,括号为传递参数值 } } } } protected FloatCryptoWrapper[] mCryptoValues;}
用法
//注册事件,并使用PlayerPropertySet PropertySet;Player.PropertySet.AddChanageEvent(EPlayerProperty.Health, OnHealthChange);void OnHealthChange(float newValue){ //写处理逻辑}
赋值、取值float test = PropertySet[EPlayerProperty.Health];PropertySet[EPlayerProperty.Health] = 1f;
0 0
- 通过枚举值实现赋值、取值、触发时间
- TextBox只读时不能通过后台赋值取值解决办法
- jquery通过name,id,class取值或赋值
- jquery 取值 赋值
- jQuery 取值赋值
- Checkbox取值赋值
- jquery取值赋值
- jQuery 取值、赋值
- js_checkbox取值赋值
- Jquery操作下拉框(DropDownList)实现取值赋值
- datawindow赋值与取值
- Jquery表单取值赋值
- Jquery表单取值赋值
- Jquery 取值,赋值汇总
- Jquery 取值,赋值总结
- jquery 取值与赋值
- CKEditor js取值赋值
- jQuery控件取值,赋值
- Hive分析窗口函数(四) LAG,LEAD,FIRST_VALUE,LAST_VALUE
- JS OffsetParent属性深入解析
- Android入门1:使用VideoView和MediController播放视频
- VS2013中编译openssl的步骤和使用设置
- swift- 去除tableView 多余行
- 通过枚举值实现赋值、取值、触发时间
- JAVA-装饰器模式
- Hive分析窗口函数(五) GROUPING SETS,GROUPING__ID,CUBE,ROLLUP
- Android Support V7 包中 ActionBar的使用
- HTML5,CSS,JAVASCRIPT相关科普
- 如何理解和熟练运用js中的call及apply?
- 使用 Spring 进行单元测试
- 不花钱开店 网上发布产品 最便宜的推广(第二章)
- Android mqtt即时通讯方案