接口操作示例

                                                                      

                                                                接口操作示例

None.gif // 一个接口定义一个协定。实现某接口的类或结构必须遵守该接口定义的协定。
None.gif    
// 一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。
None.gif
None.gif    
// 接口可以包含方法、属性、事件和索引器。接口本身不提供它所定义的成员的实现。
None.gif    
// 接口只指定实现该接口的类或结构必须提供的成员。


interface1.JPG

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
namespace  接口操作
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.RichTextBox richTextBox1;
InBlock.gif        
private System.Windows.Forms.Button button1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// richTextBox1
InBlock.gif            
// 
InBlock.gif
            this.richTextBox1.Location = new System.Drawing.Point(1616);
InBlock.gif            
this.richTextBox1.Name = "richTextBox1";
InBlock.gif            
this.richTextBox1.Size = new System.Drawing.Size(480272);
InBlock.gif            
this.richTextBox1.TabIndex = 0;
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(51232);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.Size = new System.Drawing.Size(12840);
InBlock.gif            
this.button1.TabIndex = 1;
InBlock.gif            
this.button1.Text = "运行接口程序";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(664310);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Controls.Add(this.richTextBox1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.richTextBox1.Text ="";
InBlock.gif            C_If1 If1
=new C_If1("中华人民共和国万岁!") ;
InBlock.gif            C_If2 If2
=new C_If2("世界人民大团结万岁!") ;
InBlock.gif            
this.richTextBox1.Text +=If1.PrintString("好。\n")  ;
InBlock.gif            
this.richTextBox1.Text +=If2.PrintString("好思想。将来做共产主义接班人!") ;
InBlock.gif
InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//一个接口定义一个协定。实现某接口的类或结构必须遵守该接口定义的协定。
InBlock.gif    
//一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。
InBlock.gif
InBlock.gif    
//接口可以包含方法、属性、事件和索引器。接口本身不提供它所定义的成员的实现。
InBlock.gif    
//接口只指定实现该接口的类或结构必须提供的成员。
InBlock.gif

InBlock.gif
InBlock.gif    
internal interface If_PrintString
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义属性
InBlock.gif
        string GsStr   
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
InBlock.gif            
set
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//定义方法
InBlock.gif
        string PrintString(string strPrint);
InBlock.gif
InBlock.gif        
//定义事件
InBlock.gif        
//dot.gifdot.gif
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif
InBlock.gif    
internal class C_If1 : If_PrintString
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string str1="";
InBlock.gif
InBlock.gif        
public string GsStr
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return str1;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{str1=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public C_If1(string str2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          str1
=str2;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string PrintString(string strPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "我在用C_If1打印汉字: " + GsStr +"  "+ strPrint;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
internal class C_If2 : If_PrintString
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string str1="";
InBlock.gif
InBlock.gif        
public string GsStr
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return str1;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{str1=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public C_If2(string str2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str1
=str2;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string PrintString(string strPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "我在用C_If2打印汉字: " + GsStr +"  "+ strPrint;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
//    接口可以是命名空间或类的成员,并且可以包含下列成员的签名: 
InBlock.gif
//    方法 
InBlock.gif
//    属性 
InBlock.gif
//    索引器   索引器是这样一个成员:它使对象能够用与数组相同的方式进行索引。本例使用get.set访问器
InBlock.gif
//    事件 
InBlock.gif
//    一个接口可从一个或多个基接口继承。在下例中,接口 IMyInterface 从两个基接口 IBase1 和 IBase2 继承:
InBlock.gif
//    interface IMyInterface: IBase1, IBase2
InBlock.gif
//    {
InBlock.gif
//        void MethodA();
InBlock.gif
//        void MethodB();
InBlock.gif
//    }
InBlock.gif
//    接口可以由类和结构实现。实现的接口的标识符出现在类的基列表中。例如:
InBlock.gif
//    class Class1: Iface1, Iface2
InBlock.gif
//    {
InBlock.gif
//        // class members
InBlock.gif
//    }
InBlock.gif
//    类的基列表同时包含基类和接口时,列表中首先出现的是基类。例如:
InBlock.gif
//    class ClassA: BaseClass, Iface1, Iface2 
InBlock.gif
//    {
InBlock.gif
//        // class members
InBlock.gif
//    }
InBlock.gif

ExpandedBlockEnd.gif}

None.gif

None.gif // internal 关键字是类型和类型成员的访问修饰符。内部成员只有在同一程序集中的文件内才是可访问的。
None.gif    
// 有关程序集的更多信息,请参见组件和程序集。
None.gif    
// 内部访问通常用于基于组件的开发,因为它使一组组件能够以私有方式进行合作,而不必向应用程序代码的
None.gif    
// 其余部分公开。例如,用于生成图形用户界面的框架可以提供“控件”类和“窗体”类,这些类通过使用具
None.gif    
// 有内部访问能力的成员进行合作。由于这些成员是内部的,它们不向正在使用框架的代码公开。
None.gif    
// 在定义具有内部访问能力的成员的程序集外部引用该成员是错误的。
None.gif    
// 警告   尽管不能用 C# 重写 internal virtual 方法,但可以用某些语言(如使用 Ilasm.exe 的文本 Microsoft 
None.gif    
// 中间语言 (MSIL) 重写它。
None.gif    
// 有关 internal 和其他访问修饰符的比较,请参见可访问性级别。



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值