一、新建一个接口类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// InterF 的摘要说明
/// </summary>
public class InterF
{
public interface hide
{
string show();
}
public interface display
{
string show();
}
}
二、新建接口实现类,hide.cs和display.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Speaker 的摘要说明
/// </summary>
public class hide : InterF.hide
{
public string show()
{
return "隐示接口";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Speaker1 的摘要说明
/// </summary>
public class display : InterF.display
{
string InterF.display.show()
{
return "显示接口";
}
}
三、新建一个default.aspx文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
InterF.hide c = new hide();
c.show();
hide s = new hide();
s.show();
Label1.Text = s.show() + c.show();
InterF.display c1 = new display();
c1.show();
Label2.Text = c1.show();
//下面写法将引起编译错误错误“PetShop.Speaker”不包含“Speak”的定义
//display s1 = new display();
//s1.Speak();
}
}
四、综上总结:显示接口和隐式接口的区别
<1>隐示实现接口和类都可以访问 显示实现只有接口可以访问。
<2>显示实现益处
1:隐藏代码的实现功能
2:在使用接口访问的系统中,调用者只能通过接口调用而不是底层的类来访问,有利于安全性
综上:当类或结构继承接口时,意味着该类或结构为该接口定义的所有成员提供实现。接口本身不提供类或结构能够以继承基类功能的方式继承的任何功能。基类实现接口,派生类将继承该实现