小弟最近在研究委托和接口,有点不明白的地方希望各位前辈给点指导,先谢谢了
是这样,我写了一个最简单的委托,然后想写一个接口来实现。using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace consign1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>

//定义一个接口
interface consign //接口 

 
} //定义一个委托
public delegate int text(int x,int y); public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
/// <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 );
} static int text1(int e,int r)
{
return e*r;
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(16, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 0;
this.button1.Text = "=";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(16, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(160, 32);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
// 
// textBox3
// 
this.textBox3.Location = new System.Drawing.Point(16, 136);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(240, 21);
this.textBox3.TabIndex = 1;
this.textBox3.Text = "";
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(128, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 2;
this.label1.Text = "*";
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(160, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 23);
this.button2.TabIndex = 0;
this.button2.Text = "清空";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 182);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
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 Form1_Load(object sender, System.EventArgs e)
{

} private void button1_Click(object sender, System.EventArgs e)
{
int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
text t1 = new text(text1); int temp = t1(a,b);
textBox3.Text = temp.ToString();
} private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
}
问题就是在接口中我应该写什么呢?我想把委托的方法
static int text1(int e,int r)
{
return e*r;
}
写到接口中,然后再用一个类来继承接口,但是在实现接口的时候就不知道怎么写了,如果要是这么写的话,那么委托应该传什么参呢?拜托前辈指点一二,谢谢了~

解决方案 »

  1.   

    接口中的成员方法必须是void的吗?这么说实现接口的方法也应该是void的喽?如果要是这个方法有返回值呢?我是新手,接口和委托没有意义,就是想了解一个这两个东西~其实委托就是调用一个乘法操作的方法。就这么简单。
      

  2.   

    //定义一个委托
    public delegate int text(int x,int y); public class Form1 : System.Windows.Forms.Form
    {
                 public text MyText;
             }          public class Form2 : System.Windows.Forms.Form
    {
                 public int mytext(int x,int y)
                 {
                     return x * y;
                 }
                 ...
                 Form1 form = new Form1();
                 form.MyText = new text(mytext);
             } 
      

  3.   

    谢谢楼上的前辈,那么如果要是接口实现怎么办呢?我看了好多的例子,好像接口中的成员方法都是viod,而且实现接口的方法也都是void?那么接口中怎么才能调用委托呢?我想在一个接口中调用好多的委托不知道可以吗?