帮忙去掉重复代码
时间:2011-12-06
来源:互联网
PriorityPaired和RefreshStatePaired都是继承自PairedBase。如题,去掉重复代码。实在没分,不好意思。
C# code
C# code
public class PriorityDic { private static PairedBase _paired = new PriorityPaired(); public static string GetValue(string key) { return _paired.GetValue(key); } public static string GetKey(string value) { return _paired.GetKey(value); } public static bool ContainsKey(string key) { return _paired.ContainsKey(key); } public static bool ContainsValue(string value) { return _paired.ContainsValue(value); } } public class RefreshStateDic { private static PairedBase _paired = new RefreshStatePaired(); public static string GetValue(string key) { return _paired.GetValue(key); } public static string GetKey(string value) { return _paired.GetKey(value); } public static bool ContainsKey(string key) { return _paired.ContainsKey(key); } public static bool ContainsValue(string value) { return _paired.ContainsValue(value); } }
作者: wxssaa 发布时间: 2011-12-06
用接口..interface
作者: enter89 发布时间: 2011-12-06
叫重构吧,兄弟
看不明白你的代码目的
看不明白你的代码目的
作者: jiuhexuan 发布时间: 2011-12-06
发现静态的方法不能override,于是写了个非静态的PairedBase类,继承PairedBase的类可以添加自动以的字典。
想静态使用GetValue()和GetKey()方法,于是又包了一层PriorityDic。我把全部代码贴过来吧,就是长点。
[code=C#][/code]
public abstract class PairedBase
{
protected Dictionary<string, string> _dictionary = new Dictionary<string, string>();
protected PairedBase()
{
Initialize();
}
protected abstract void Initialize();
public string GetValue(string key)
{
return _dictionary.ContainsKey(key) ? _dictionary[key] : string.Empty;
}
public string GetKey(string value)
{
if (!_dictionary.ContainsValue(value))
return string.Empty;
var item = _dictionary.Where(c => c.Value == value);
return item.Count() > 0 ? item.FirstOrDefault().Key : string.Empty;
}
public bool ContainsKey(string key)
{
return _dictionary.ContainsKey(key);
}
public bool ContainsValue(string value)
{
return _dictionary.ContainsValue(value);
}
}
public class PriorityPaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("高", "1");
_dictionary.Add("中", "2");
_dictionary.Add("低", "3");
}
}
public class RefreshStatePaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("发生更新", "1");
_dictionary.Add("正则表达式失效", "2");
_dictionary.Add("网站响应超时", "3");
_dictionary.Add("网站出现异常", "4");
_dictionary.Add("数据库信息有误", "5");
}
}
public class SoftStatePaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("自动刷新", "1");
_dictionary.Add("手动刷新", "2");
_dictionary.Add("不需要刷新", "3");
_dictionary.Add("软件停运", "4");
}
}
public class PriorityDic
{
private static PairedBase _paired = new PriorityPaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
public class RefreshStateDic
{
private static PairedBase _paired = new RefreshStatePaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
public class SoftStateDic
{
private static PairedBase _paired = new SoftStatePaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
想静态使用GetValue()和GetKey()方法,于是又包了一层PriorityDic。我把全部代码贴过来吧,就是长点。
[code=C#][/code]
public abstract class PairedBase
{
protected Dictionary<string, string> _dictionary = new Dictionary<string, string>();
protected PairedBase()
{
Initialize();
}
protected abstract void Initialize();
public string GetValue(string key)
{
return _dictionary.ContainsKey(key) ? _dictionary[key] : string.Empty;
}
public string GetKey(string value)
{
if (!_dictionary.ContainsValue(value))
return string.Empty;
var item = _dictionary.Where(c => c.Value == value);
return item.Count() > 0 ? item.FirstOrDefault().Key : string.Empty;
}
public bool ContainsKey(string key)
{
return _dictionary.ContainsKey(key);
}
public bool ContainsValue(string value)
{
return _dictionary.ContainsValue(value);
}
}
public class PriorityPaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("高", "1");
_dictionary.Add("中", "2");
_dictionary.Add("低", "3");
}
}
public class RefreshStatePaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("发生更新", "1");
_dictionary.Add("正则表达式失效", "2");
_dictionary.Add("网站响应超时", "3");
_dictionary.Add("网站出现异常", "4");
_dictionary.Add("数据库信息有误", "5");
}
}
public class SoftStatePaired : PairedBase
{
protected override void Initialize()
{
_dictionary.Add("自动刷新", "1");
_dictionary.Add("手动刷新", "2");
_dictionary.Add("不需要刷新", "3");
_dictionary.Add("软件停运", "4");
}
}
public class PriorityDic
{
private static PairedBase _paired = new PriorityPaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
public class RefreshStateDic
{
private static PairedBase _paired = new RefreshStatePaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
public class SoftStateDic
{
private static PairedBase _paired = new SoftStatePaired();
public static string GetValue(string key)
{
return _paired.GetValue(key);
}
public static string GetKey(string value)
{
return _paired.GetKey(value);
}
public static bool ContainsKey(string key)
{
return _paired.ContainsKey(key);
}
public static bool ContainsValue(string value)
{
return _paired.ContainsValue(value);
}
}
引用 2 楼 jiuhexuan 的回复:
叫重构吧,兄弟
看不明白你的代码目的
叫重构吧,兄弟
看不明白你的代码目的
作者: wxssaa 发布时间: 2011-12-06
重贴一下
C# code
C# code
public abstract class PairedBase { protected Dictionary<string, string> _dictionary = new Dictionary<string, string>(); protected PairedBase() { Initialize(); } protected abstract void Initialize(); public string GetValue(string key) { return _dictionary.ContainsKey(key) ? _dictionary[key] : string.Empty; } public string GetKey(string value) { if (!_dictionary.ContainsValue(value)) return string.Empty; var item = _dictionary.Where(c => c.Value == value); return item.Count() > 0 ? item.FirstOrDefault().Key : string.Empty; } public bool ContainsKey(string key) { return _dictionary.ContainsKey(key); } public bool ContainsValue(string value) { return _dictionary.ContainsValue(value); } } public class PriorityPaired : PairedBase { protected override void Initialize() { _dictionary.Add("高", "1"); _dictionary.Add("中", "2"); _dictionary.Add("低", "3"); } } public class RefreshStatePaired : PairedBase { protected override void Initialize() { _dictionary.Add("发生更新", "1"); _dictionary.Add("正则表达式失效", "2"); _dictionary.Add("网站响应超时", "3"); _dictionary.Add("网站出现异常", "4"); _dictionary.Add("数据库信息有误", "5"); } } public class PriorityDic { private static PairedBase _paired = new PriorityPaired(); public static string GetValue(string key) { return _paired.GetValue(key); } public static string GetKey(string value) { return _paired.GetKey(value); } public static bool ContainsKey(string key) { return _paired.ContainsKey(key); } public static bool ContainsValue(string value) { return _paired.ContainsValue(value); } } public class RefreshStateDic { private static PairedBase _paired = new RefreshStatePaired(); public static string GetValue(string key) { return _paired.GetValue(key); } public static string GetKey(string value) { return _paired.GetKey(value); } public static bool ContainsKey(string key) { return _paired.ContainsKey(key); } public static bool ContainsValue(string value) { return _paired.ContainsValue(value); } }
作者: wxssaa 发布时间: 2011-12-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28