博文

C#语法详细解释---接口[4](2008-09-17 01:52:00)

摘要:1.1 接口实现 接口可以由类和结构来实现。为了指示类或结构实现了某接口,在该类或结构的基类列表中应该包含该接口的标识符。例如: interface ICloneable
{
object Clone();
} interface IComparable
{
int CompareTo(object other);
} class ListEntry: ICloneable, IComparable
{
public object Clone() {...} public int CompareTo(object other) {...}
} 如果一个类或结构实现某接口,则它还隐式实现该接口的所有基接口。即使在类或结构的基类列表中没有显式列出所有基接口,也是这样。例如: interface IControl
{
void Paint();
} interface ITextBox: IControl
{
void SetText(string text);
} class TextBox: ITextBox
{
public void Paint() {...} public void SetText(string text) {...}
} 在此,类 TextBox 同时实现了 IControl 和 ITextBox。 1.1.1 显式接口成员实现 为了实现接口,类或结构可以声明显式接口成员实现 (explicit interface member implementation)。显式接口成员实现就是一种方法、属性、事件或索引器声明,它使用完全限定接口成员名称作为标识符。例如 interface ICloneable
{
object Clone();
} interface IComparable
{
int CompareTo(object other);
} class ListEntry: ICloneable, IComparable
{
object ICloneable.Clone() {...} int IComparable.CompareTo(object ......

阅读全文(2385) | 评论:1

C#语法详细解释---接口[3](2008-09-17 01:51:00)

摘要:调用 n.Add(1) 选择 IInteger.Add,方法是应用 错误!未找到引用源。 节的重载决策规则。类似的,调用 n.Add(1.0) 选择 IDouble.Add。插入显式强制转换后,就只有一个候选方法了,因此没有多义性。 在下面的示例中 interface IBase
{
void F(int i);
} interface ILeft: IBase
{
new void F(int i);
} interface IRight: IBase
{
void G();
} interface IDerived: ILeft, IRight {} class A
{
void Test(IDerived d) {
     d.F(1);                 // Invokes ILeft.F
     ((IBase)d).F(1);        // Invokes IBase.F
     ((ILeft)d).F(1);        // Invokes ILeft.F
     ((IRight)d).F(1);       // Invokes IBase.F
}
} IBase.F 成员被 ILeft.F 成员隐藏。因此,即使在通过 IRight 的访问路径中 IBase.F 似乎没有被隐藏,调用 d.F(1) 仍选择 ILeft.F。 多重继承接口中的直观隐藏规则简单地说就是:如果成员在任何一个访问路径中被隐藏,那么它在所有访问路径中都被隐藏。由于从 IDerived 经 ILeft 到 IBase......

阅读全文(2325) | 评论:0

C#语法详细解释---接口[2](2008-09-17 01:51:00)

摘要:int Count { get; } event StringListEvent Changed; string this[int index] { get; set; }
} 声明了一个接口,该接口的成员涵盖了所有可能作为接口成员的种类:方法、属性、事件和索引器。 interface-declaration 创建新的声明空间(第 错误!未找到引用源。 节),并且 interface-declaration 直接包含的 interface-member-declarations 将新成员提供给该声明空间。以下规则适用于 interface-member-declaration: ·          方法的名称必须与同一接口中声明的所有属性和事件的名称不同。此外,方法的签名(第 错误!未找到引用源。 节)必须不同于在同一接口中声明的所有其他方法的签名,并且在同一接口中声明的两种方法的签名不能只有 ref 和 out 不同。 ·          属性或事件的名称必须与同一接口中声明的所有其他成员的名称不同。 ·          一个索引器的签名必须区别于在同一接口中声明的其他所有索引器的签名。 准确地说,接口所继承的成员不是该接口的声明空间的一部分。因此,允许接口用与它所继承的成员相同的名称或签名来声明新的成员。发生这种情况时,则称派生的接口成员隐藏了基接口成员。隐藏一个继承的成员不算是错误,但这确实会导致编译器发出警告。为了避免出现上述警告,派生接口成员的声明中必须包含一个 new 修饰符,以指示该派生成员将要隐藏对应的基成员。第 错误!未找到引用源。 节中对该主题进行了进一步讨论。 如果在不隐藏所继承成员的声明中包含 new 修饰符,将对此状况发出警告。通过移除 new 修饰符可取消显示此警告。 请注意,严格来讲,类 object 中的成员不是任何接口的成员(第 1.2 节)。但是,通过在任何接口类型中进行成员查找,可获得类 object 中的成员(第 错误!未找到引用......

阅读全文(2643) | 评论:0

C#语法详细解释---接口[1](2008-09-17 01:50:00)

摘要:              接口 一个接口定义一个协定。实现某接口的类或结构必须遵守该接口定义的协定。一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。 接口可以包含方法、属性、事件和索引器。接口本身不提供它所定义的成员的实现。接口只指定实现该接口的类或结构必须提供的成员。 1.1 接口声明 interface-declaration 是用于声明新的接口类型的 type-declaration(第 错误!未找到引用源。 节)。 interface-declaration:
attributesopt   interface-modifiersopt   interface   identifier   interface-baseopt   interface-body   ;opt interface-declaration 由下列项组成:一个可选的 attributes 集(第 错误!未找到引用源。 节),后跟一个可选的 interface-modifiers 集(第 1.1.1 节),后面再跟关键字 interface 和命名接口的 identifier,还可根据需要后跟可选的 interface-base 规范(第 1.1.2 节),再后跟 interface-body(第 1.1.3 节),还可选择后跟一个分号。 1.1.1 接口修饰符 interface-declaration 可以根据需要包含一个接口修饰符序列: interface-modifiers:
interface-modifier
interface-modifiers   interface-modifier interface-modifier:
new
public
protected
internal
private 同一修饰符在一个接口声明中出现多次属于编译时错误。 new 修饰符仅允许在类中定义的接口中使用。它指定接口隐藏同名的继承......

阅读全文(2615) | 评论:0

C# 设计模式  --举例(2008-08-03 02:41:00)

摘要:在设计模式中有一种模式叫Builder模式,其原理如下: 我们可以将Builder理解成电饭锅,给这个Builder放进去米和水,经过Builder的Build后,我们就可以取出香喷喷的米饭了。
C#中有一个类叫StringBuilder,输入必要的信息后,就可以取出对应的String。其使用方法如下: using System;
using System.Text;

class Exam
{
 public static void Main()
  {
  StringBuilder sb = new StringBuilder();
  sb.Append('a',2);
  sb.Append('b',3);
  sb.Append('c',4);
  Console.WriteLine(sb.ToString()); //打印出 aabbbcccc
  sb.Remove(0, sb.Length); //清除sb中的所有信息
 }
} 程序执行结果为: aabbbcccc
请使用StringBuilder对以下打印三角型的程序进行改写,写出新程序。 using System;
public class Exam
{
 public static void Main()
  {
  Console.Write("请输入行数:");
  int lines = int.Parse(Console.ReadLine());
  Console.WriteLine("");
  for(int i=1; i<=lin......

阅读全文(1489) | 评论:0

C#语法详细解释---委托(2008-08-03 02:29:00)

摘要: 1. 委托 委托是用来处理其他语言(如 C++、Pascal 和 Modula)需用函数指针来处理的情况的。不过与 C++ 函数指针不同,委托是完全面向对象的;另外,C++ 指针仅指向成员函数,而委托同时封装了对象实例和方法。 委托声明定义一个从 System.Delegate 类派生的类。委托实例封装了一个调用列表,该列表列出了一个或多个方法,每个方法称为一个可调用实体。对于实例方法,可调用实体由该方法和一个相关联的实例组成。对于静态方法,可调用实体仅由一个方法组成。用一个适当的参数集来调用一个委托实例,就是用此给定的参数集来调用该委托实例的每个可调用实体。 委托实例的一个有趣且有用的属性是:它不知道也不关心它所封装的方法所属的类;它所关心的仅限于这些方法必须与委托的类型兼容(第 15.1 节)。这使委托非常适合于“匿名”调用。 1.1 委托声明 delegate-declaration 是一种 type-declaration(第 9.5 节),它声明一个新的委托类型。 delegate-declaration:
attributesopt   delegate-modifiersopt   delegate   return-type   identifier
             (   formal-parameter-listopt   )   ; delegate-modifiers:
delegate-modifier
delegate-modifiers   delegate-modifier delegate-modifier:
new
public
protected
internal
private 同一修饰符在一个委托声明中多次出现属于编译时错误。 new 修饰符仅允许在其他类型中声明的委托上使用,在这种情况下该修饰符表示所声明的委托会隐藏具有相同名称的继承成员,详见第 10.2.2 ......

阅读全文(3147) | 评论:0

汇总c#.net常用函数和方法集(2008-08-29 13:32:00)

摘要:汇总c#.net常用函数和方法集
  1、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
2、Int32.Parse(变量) Int32.Parse("常量")
字符型转换 转为32位数字型 3、 变量.ToString()
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
4、变量.Length 数字型
取字串长度:
如: string str="中国";
int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名 5、System.Text.Encoding.Default.GetBytes(变量)
字码转换 转为比特码
如:byte[] by......

阅读全文(1605) | 评论:0

asp.net常用函数精粹[3](2008-07-24 10:22:00)

摘要:22.Panel 横向滚动,纵向自动扩展   <asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>   23.回车转换成Tab (1)     <script language="javascript" for="document" event="onkeydown">   if(event.keyCode==13 && event.srcElement.type!=’button’ && event.srcElement.type!=’submit’ &&     event.srcElement.type!=’reset’ && event.srcElement.type!=’’&& event.srcElement.type!=’textarea’);   event.keyCode=9; </script>   (2)  //当在有keydown事件的控件上敲回车时,变为tab   public void Tab(System.Web .UI.WebControls .WebControl webcontrol) { webcontrol.Attributes .Add ("onkeydown", "if(event.keyCode==13) event.keyCode=9"); } 24.DataGrid超级连接列 DataNavigateUrlField="字段名" DataNavigateUrlFormatString="http://xx/inc/delete.aspx?ID={0}"   25.DataGrid行随鼠标变色   private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {   if (e.Item.ItemType!=ListItemType.Header)   {   e.Item.......

阅读全文(1542) | 评论:0

asp.net常用函数精粹[2](2008-07-24 10:21:00)

摘要:11.//获取" . "后面的字符   i.ToString().Trim().Substring(i.ToString().Trim().LastIndexOf(".")+1).ToLower().Trim()   12. 打开新的窗口并传送参数:   传送参数:   response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")   接收参数:   string a = Request.QueryString("id"); string b = Request.QueryString("id1");   12.为按钮添加对话框   Button1.Attributes.Add("onclick","return confirm(’确认?’)"); button.attributes.add("onclick","if(confirm(’are you sure...?’)){return true;}else{return false;}")   13.删除表格选定记录   int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex]; string deleteCmd = "Delete from Employee where emp_id = " + intEmpID.ToString()   14.删除表格记录警告   private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e) {   switch(e.Item.ItemType)   {   case ListItemType.Item :   case ListItemType.AlternatingItem :   case ListItemType.EditItem:   TableCell myTableCell;   myT......

阅读全文(1619) | 评论:0

asp.net常用函数精粹[1](2008-09-18 16:17:00)

摘要:1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script>"); Response.Write("<script>window.location ='http://www.cgy.cn/bizpulic/upmeb.aspx'</script>");   2.//弹出对话框   Response.Write("<script language='javascript'>alert('产品添加成功!')</script >");   3.//删除文件     string filename ="20059595157517.jpg"; pub.util.DeleteFile(HttpContext.Current.Server.MapPath("../file/")+filename);   4.//绑定下拉列表框datalist   System.Data.DataView dv=conn.Exec_ex("select -1 as code,'请选择经营模式' as content from dealin union select code,content from dealin"); this.dealincode.DataSource=dv; this.dealincode.DataTextField="content"; this.dealincode.DataValueField="code";    this.dealincode.DataBind(); this.dealincode.Items.FindByValue(dv[0]["dealincode"].ToString()).Selected=true;   5.//时间去秒显示   <%# System.DateTime.Parse(DataBinder.Eval(Container.DataItem,"begtime").ToStrin......

阅读全文(1818) | 评论:0