首先,我们来看看“星期”的例子:
以下是微软的DayOfWeek:
/// <summary>
/// Specifies the day of the week.
/// </summary>
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
没有什么特别之处。歪果仁喜欢以星期日作为一周的开头,想起一周刚刚开始就休假,很开心不是?:)-
如果想表示周末,要怎样处理更好?
我们可以改进一下,比如:
/// <summary>
/// Specifies the day of the week.
/// </summary>
public enum DayOfWeekExt
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
WeekEnd = Sunday|Saturday
}
注意上面的变化,我们将Sunday的值设为1(2^0),Monday=2(2^1), Tuesday=4(2^2)...,其中2^2表示2的2次方,其他类推。
其中,WeekEnd用“Sunday|Saturday”表示其值(其中Sunday的值为1,Saturday的值为64),WeekEnd的值为:1+64=65,说明含有周六(Saturday)和周日(Sunday)。
您可以用以下代码测试它的值是否为65:
if ((int)DayOfWeekExt.WeekEnd == 65)
Console.WriteLine("WeekEnd=65");
else
Console.WriteLine("WeekEnd!=65, Value="+((int)DayOfWeekExt.WeekEnd).ToString());
具“中国特色”的周末来了!
有些中国公司或工厂可能会实行“单双周末轮休”制度,当然,也有极少数厂家“有点黑”:永远只有周日。就说说“单双周末轮休”(俗称“大小周”)的事吧。
为了描述方便,我们约定:“大小周”分为大周和小周,大周休假两天(周六、周日),小周休假一天(仅休周日)。
“大周”、“小周”极具中国特色,估计没有确切的英文,我们暂定英文名称为:大周(WeekEndMax),小周(WeekEndMin)。
修改后的枚举如下:
public enum DayOfWeekExt
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
WeekEndMin = Sunday,
WeekEndMax = Sunday | Saturday
}
可以看到,上面去掉了WeekEnd,新增了两行,注意赋值方法:
WeekEndMin = Sunday,
WeekEndMax = Sunday | Saturday
将周日赋值给了小周,周六周日赋值给了大周。
您可以用以下代码测试它的值是否为65:
if ((int)DayOfWeekExt.WeekEndMax == 65)
Console.WriteLine("WeekEndMax=65");
else
Console.WriteLine("WeekEndMax!=65, Value="+((int)DayOfWeekExt.WeekEndMax).ToString());
再看一个用于内容对齐的例子,ContentAlignment枚举:
//
// 摘要:
// Specifies alignment of content on the drawing surface.
[Editor("System.Drawing.Design.ContentAlignmentEditor, System.Drawing.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public enum ContentAlignment
{
//
// 摘要:
// Content is vertically aligned at the top, and horizontally aligned on the left.
TopLeft = 1,
//
// 摘要:
// Content is vertically aligned at the top, and horizontally aligned at the center.
TopCenter = 2,
//
// 摘要:
// Content is vertically aligned at the top, and horizontally aligned on the right.
TopRight = 4,
//
// 摘要:
// Content is vertically aligned in the middle, and horizontally aligned on the
// left.
MiddleLeft = 16,
//
// 摘要:
// Content is vertically aligned in the middle, and horizontally aligned at the
// center.
MiddleCenter = 32,
//
// 摘要:
// Content is vertically aligned in the middle, and horizontally aligned on the
// right.
MiddleRight = 64,
//
// 摘要:
// Content is vertically aligned at the bottom, and horizontally aligned on the
// left.
BottomLeft = 256,
//
// 摘要:
// Content is vertically aligned at the bottom, and horizontally aligned at the
// center.
BottomCenter = 512,
//
// 摘要:
// Content is vertically aligned at the bottom, and horizontally aligned on the
// right.
BottomRight = 1024
}
以九宫格为例说明对齐方式:
TopLeft 左上 |
TopCenter 中上 |
TopRight 右上 |
MiddleLeft 左中 |
MiddleCenter 正中 |
MiddleRight 右中 |
BottomLeft 左下 |
BottomCenter 中下 |
BottomRight 右下 |
接下来,我们需要“位运算+枚举”来做一些事。位运算见下表,如果不明白的同道兄弟姐妹,需要“补补气血”,嘿嘿!
符号 |
描述 |
运算规则 |
& |
与 |
两个位都为1时,结果才为1 |
| |
或 |
两个位都为0时,结果才为0 |
^ |
异或 |
两个位相同为0,相异为1 |
~ |
取反 |
0变1,1变0 |
<< |
左移 |
各二进位全部左移若干位,高位丢弃,低位补0 |
>> |
右移 |
各二进位全部右移若干位,对无符号数,高位补0,有符号数,各编译器处理方法不一样,有的补符号位(算术右移),有的补0(逻辑右移) |
(未完,先做事,有空再继续......)