接口操作示例 //一个接口定义一个协定。实现某接口的类或结构必须遵守该接口定义的协定。 //一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。 //接口可以包含方法、属性、事件和索引器。接口本身不提供它所定义的成员的实现。 //接口只指定实现该接口的类或结构必须提供的成员。 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace 接口操作{ /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button1; /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(16, 16); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(480, 272); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(512, 32); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(128, 40); this.button1.TabIndex = 1; this.button1.Text = "运行接口程序"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(664, 310); this.Controls.Add(this.button1); this.Controls.Add(this.richTextBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { this.richTextBox1.Text =""; C_If1 If1=new C_If1("中华人民共和国万岁!") ; C_If2 If2=new C_If2("世界人民大团结万岁!") ; this.richTextBox1.Text +=If1.PrintString("好。\n") ; this.richTextBox1.Text +=If2.PrintString("好思想。将来做共产主义接班人!") ; } private void Form1_Load(object sender, System.EventArgs e) { } } //一个接口定义一个协定。实现某接口的类或结构必须遵守该接口定义的协定。 //一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。 //接口可以包含方法、属性、事件和索引器。接口本身不提供它所定义的成员的实现。 //接口只指定实现该接口的类或结构必须提供的成员。 internal interface If_PrintString { //定义属性 string GsStr { get; set; } //定义方法 string PrintString(string strPrint); //定义事件 // } internal class C_If1 : If_PrintString { private string str1=""; public string GsStr { get{return str1;} set{str1=value;} } public C_If1(string str2) { str1=str2; } public string PrintString(string strPrint) { return "我在用C_If1打印汉字: " + GsStr +" "+ strPrint; } } internal class C_If2 : If_PrintString { private string str1=""; public string GsStr { get{return str1;} set{str1=value;} } public C_If2(string str2) { str1=str2; } public string PrintString(string strPrint) { return "我在用C_If2打印汉字: " + GsStr +" "+ strPrint; } }// 接口可以是命名空间或类的成员,并且可以包含下列成员的签名: // 方法 // 属性 // 索引器 索引器是这样一个成员:它使对象能够用与数组相同的方式进行索引。本例使用get.set访问器// 事件 // 一个接口可从一个或多个基接口继承。在下例中,接口 IMyInterface 从两个基接口 IBase1 和 IBase2 继承:// interface IMyInterface: IBase1, IBase2// {// void MethodA();// void MethodB();// }// 接口可以由类和结构实现。实现的接口的标识符出现在类的基列表中。例如:// class Class1: Iface1, Iface2// {// // class members// }// 类的基列表同时包含基类和接口时,列表中首先出现的是基类。例如:// class ClassA: BaseClass, Iface1, Iface2 // {// // class members// }} //internal 关键字是类型和类型成员的访问修饰符。内部成员只有在同一程序集中的文件内才是可访问的。 //有关程序集的更多信息,请参见组件和程序集。 //内部访问通常用于基于组件的开发,因为它使一组组件能够以私有方式进行合作,而不必向应用程序代码的 //其余部分公开。例如,用于生成图形用户界面的框架可以提供“控件”类和“窗体”类,这些类通过使用具 //有内部访问能力的成员进行合作。由于这些成员是内部的,它们不向正在使用框架的代码公开。 //在定义具有内部访问能力的成员的程序集外部引用该成员是错误的。 //警告 尽管不能用 C# 重写 internal virtual 方法,但可以用某些语言(如使用 Ilasm.exe 的文本 Microsoft //中间语言 (MSIL) 重写它。 //有关 internal 和其他访问修饰符的比较,请参见可访问性级别。