如何用interface实现多继承?在线等

hater 2003-05-16 12:20:00
请写一段代码并加上注释,最好详细点谢谢。
收到马上给分
...全文
128 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hater 2003-05-21
  • 打赏
  • 举报
回复
结贴
windsoft 2003-05-16
  • 打赏
  • 举报
回复
這麼復雜
public class aa : i1,i2
i1,i2是interface
就行了吧
jiezhi 2003-05-16
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinApp_Interface_Document
{
/// <summary>
/// Define a interface IStorable
/// </summary>
interface IStorable
{
void Read(TextBox txt_box);
void Write(TextBox txt_box);
int Status{get;set;}
}
interface ICompressible
{
void Compress(TextBox txt_box);
void Decompress(TextBox txt_box);
}
public class Document:IStorable,ICompressible
{
public Document(string s)
{
MessageBox.Show(s);
}
//Implement method:Read()
public void Read(TextBox txt_box)
{
txt_box.AppendText("Read Document...\n");
}
public void Write(TextBox txt_box)
{
txt_box.AppendText("Write Document...\n");
}
public void Compress(TextBox txt_box)
{
txt_box.AppendText("Compress Document...\n");
}
public void Decompress(TextBox txt_box)
{
txt_box.AppendText("Decompress Document...\n");
}
//Implement property
public int Status
{
get
{
return status;
}
set
{
status=value;
}

}
private int status;

}
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <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 );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Interface";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(336, 144);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 221);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text="";
Document doc=new Document("Test Document now...");
doc.Status=-1;
doc.Read(textBox1);
textBox1.AppendText("Status:"+doc.Status.ToString()+"\n");
doc.Compress(textBox1);
doc.Decompress(textBox1);
//Conver to interface
IStorable isDoc=doc as IStorable;
if (isDoc !=null)
{
isDoc.Status=0;
isDoc.Read(textBox1);
textBox1.AppendText("Status:"+isDoc.Status.ToString()+"\n");
}
else
MessageBox.Show("Interface IStorable not supported.");
ICompressible isCom=doc as ICompressible;
if (isCom !=null)
{
isCom.Compress(textBox1);
isCom.Decompress(textBox1);
}
else
MessageBox.Show("Interface ICompressible not supported.");
}
}
}
yqdeng 2003-05-16
  • 打赏
  • 举报
回复
和c++中的class多继承差不多,只不过在继承类里面必须实现interface里得所有方法
yarshray 2003-05-16
  • 打赏
  • 举报
回复
思想:继承接口并组合实现

例子:


http://www.csdn.net/develop/read_article.asp?id=17661

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧