(精华)2020年7月22日 C#基础知识点 Flags特性
【摘要】
public enum Equipment
{
刀,
枪,
箭,
弓
}
public sealed class Player
{
internal Equipm...
public enum Equipment
{
刀,
枪,
箭,
弓
}
public sealed class Player
{
internal Equipment equipmentState;//用于描述角色可以使用哪些武器
//......其它成员
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
namespace UsageOfFlagsAttribute
{
[Flags]
public enum Equipment
{
NONE = 0x0000,
刀 = 0x0001,
枪 = 0x0002,
箭 = 0x0004,
弓 = 0x0008
}
public sealed class Player
{
internal Equipment equipmentState;//用于描述角色可以使用哪些武器
}
class Program
{
static void Main(string[] args)
{
Player player = new Player();
player.equipmentState = Equipment.刀 | Equipment.弓; //可以表示成多种状态的集合
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
用Flags标记后也就可以按位计算了,一般枚举有3种按位计算。
按位或(“|”):将指定的符号加入到枚举实例中
按位与(”&“):判断枚举实例中是否有指定的符号
异或(”^“):删除枚举实例中指定的符号
class Program
{
static void Main(string[] args)
{
Player player = new Player();
//按位或
player.equipmentState = Equipment.刀;
player.equipmentState = player.equipmentState | Equipment.弓;
Console.WriteLine(player.equipmentState.ToString());//打印结果:刀,弓
//按位与
player.equipmentState = Equipment.刀 | Equipment.弓;
player.equipmentState = player.equipmentState & Equipment.刀;
Console.WriteLine(player.equipmentState.ToString()); //打印结果 : 刀
//异或
player.equipmentState = Equipment.刀 | Equipment.弓;
player.equipmentState = player.equipmentState ^ Equipment.弓;
Console.WriteLine(player.equipmentState.ToString()); //打印结果 : 刀
Console.ReadKey();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
但是每次都要写 player.equipmentState = player.equipmentState ^ Equipment.弓; 这么赋值代码的可读性不好,而且容易浪费时间。因此可以扩展方法来封装这些操作。
public static class EnumExtendMethods
{
/// <summary>
/// 判断位标致的枚举实例中是否有指定符号
/// </summary>
/// <param name="eqipment">实例</param>
/// <param name="checkState">指定的符号</param>
/// <returns></returns>
public static bool Contains(this Equipment equipment,Equipment checkState)
{
if (checkState == 0)
{
throw new ArgumentOutOfRangeException("checkState", "不能为NONE");
}
return (equipment & checkState) == checkState;
}
/// <summary>
/// 将指定的符号从枚举实例中移除
/// </summary>
/// <param name="equipment"></param>
/// <param name="removeState">指定要移除的符号</param>
/// <returns></returns>
public static Equipment Remove(this Equipment equipment, Equipment removeState)
{
return equipment ^ removeState;
}
/// <summary>
/// 向枚举实例中加入指定的符号
/// </summary>
/// <param name="equipment"></param>
/// <param name="addState"></param>
/// <returns></returns>
public static Equipment Append(this Equipment equipment, Equipment addState)
{
return equipment | addState;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
现在可以这样调用了
class Program
{
static void Main(string[] args)
{
Player player = new Player();
//按位或
player.equipmentState = Equipment.刀;
player.equipmentState = player.equipmentState.Append(Equipment.弓);
Console.WriteLine(player.equipmentState.ToString());//打印结果:刀,弓
//按位与
player.equipmentState = Equipment.刀 | Equipment.弓;
if (player.equipmentState.Contains(Equipment.刀))
{
Console.WriteLine(player.equipmentState.ToString()); //打印结果 : 刀,弓
}
//异或
player.equipmentState = Equipment.刀 | Equipment.弓;
player.equipmentState = player.equipmentState.Remove(Equipment.弓);
Console.WriteLine(player.equipmentState.ToString()); //打印结果 : 刀
Console.ReadKey();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/107511881
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)