1.类和结构的区别
1.1 不同点:
类型:类是引用类型,结构是值类型.
继承:类可继承,结构不能继承.结构不能继承其他的结构和类,或是被用作其他结构或类的基类.
构造方法与析够函数:结构可以定义构造方法,但不能定义析够函数.类既可以定义构造方法,也可定义析够函数.
1.2相同点
对象创建:可以使用new进行初始化,或进行手工初始化.类和结构都可以.
结构与接口:结构可以实现一个或多个接口. 如上:public struct BookComponent:IBook{ //....}.
多态: 结构与接口之间是支持多态的.如上面的结构实现接口为例,多态:IBook book = new BookComponent();
2.枚举
2.1
public enum Season
{
SPRING, //春天
SUMMER, //夏天
AUTUMN, //秋天
WINTER //冬天
}
public enum SeasonTwo
{
SPRING = 0, //春天
SUMMER = 1, //夏天
AUTUMN = 2, //秋天
WINTER = 3 //冬天
}
用到时Console.WriteLine(Season.AUTUMN);
Console.WriteLine(SeasonTwo.AUTUMN);
FontStyle BtnFont = ( FontStyle)(Enum.Parse(typeof(FontStyle),“Bold”));
简单文本编辑器
private void SetStyle(object sender,EventArgs e)
{
ToolStripButton btn = sender as ToolStripButton;
FontStyle fontStyleContent = this.rchTxtContent.SelectionFont.Style;
FontStyle BtnFont = ( FontStyle)(Enum.Parse(typeof(FontStyle),btn.Tag.ToString()));
if ((fontStyleContent | BtnFont) == fontStyleContent)
{
fontStyleContent = ~BtnFont & fontStyleContent;
}
else
{
fontStyleContent = fontStyleContent | BtnFont;
}
this.rchTxtContent.SelectionFont = new Font(this.rchTxtContent.SelectionFont.FontFamily,
this.rchTxtContent.SelectionFont.Size,
fontStyleContent,
this.rchTxtContent.SelectionFont.Unit);
}
{
ToolStripButton btn = sender as ToolStripButton;
FontStyle fontStyleContent = this.rchTxtContent.SelectionFont.Style;
FontStyle BtnFont = ( FontStyle)(Enum.Parse(typeof(FontStyle),btn.Tag.ToString()));
if ((fontStyleContent | BtnFont) == fontStyleContent)
{
fontStyleContent = ~BtnFont & fontStyleContent;
}
else
{
fontStyleContent = fontStyleContent | BtnFont;
}
this.rchTxtContent.SelectionFont = new Font(this.rchTxtContent.SelectionFont.FontFamily,
this.rchTxtContent.SelectionFont.Size,
fontStyleContent,
this.rchTxtContent.SelectionFont.Unit);
}
2.2对象的形式实现
何为对象的形式实现呢? 其实这个很简单,也就是将需要枚举的标识定义在对象里,通过对象的特性将其封装.详细如下代码
public class SeasonOne
{
public static readonly SeasonOne SPRING = new SeasonOne("春天");
public static readonly SeasonOne SUMMER = new SeasonOne("夏天");
public static readonly SeasonOne AUTUMN = new SeasonOne("秋天");
public static readonly SeasonOne WINTER = new SeasonOne("冬天");
public SeasonOne(string name)
{
this.name = name;
}
//成员
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
用到时 Console.WriteLine(SeasonOne.AUTUMN.Name);
{
public static readonly SeasonOne SPRING = new SeasonOne("春天");
public static readonly SeasonOne SUMMER = new SeasonOne("夏天");
public static readonly SeasonOne AUTUMN = new SeasonOne("秋天");
public static readonly SeasonOne WINTER = new SeasonOne("冬天");
public SeasonOne(string name)
{
this.name = name;
}
//成员
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
用到时 Console.WriteLine(SeasonOne.AUTUMN.Name);
3.java中枚举的高级用法(略,见下链接)
来自:http://www.cnblogs.com/beniao/archive/2008/07/27/1249029.html