日期:2014-05-18  浏览次数:20792 次

显示实现接口,是咋回事?
如题。

------解决方案--------------------
就是实现接口定义好的
------解决方案--------------------
接口的实现分显示和隐式两种。
显示实现的接口,接口的成员都要声明成公用的(public)
而隐式实现的接口,接口的成员是接口类型加成员名称声明的。这样的声明可以通过强制转换进行和接品的转换。

------解决方案--------------------
楼上的, 你写的两个例子明明一样啊.....
------解决方案--------------------
- -例子一样
------解决方案--------------------
//隐式实现的接口类应该这样把
class b : IA
{

void a()
{
throw new Exception( "The method or operation is not implemented. ");
}
}

------解决方案--------------------
上面的两个例子是不是有点问题?
------解决方案--------------------
//接口定义
interface IA
{
void a();
}
//显示实现的接口类
class a : IA
{

void a()
{
throw new Exception( "The method or operation is not implemented. ");
}
}
//隐式实现的接口类
class b : IA
{

void IA.a()
{
throw new Exception( "The method or operation is not implemented. ");
}
}


是这样么
------解决方案--------------------
楼主说的不准确吧
http://msdn2.microsoft.com/en-us/library/ms173157(VS.80).aspx
------解决方案--------------------
using System ;
interface ICanvas
{
void Paint();
}
public class EditBox: ICanvas
{
public void Paint()//隐式-----1
{
Console.WriteLine( "Paint method is called! ");
}
void ICanvas.Paint()//显示-----2
{
Console.WriteLine( "ICanvas.Paint method is called! ");
}
}
class Test
{
static void Main()
{
EditBox editbox = new EditBox();

editbox.Paint();//调用方法1
((ICanvas)editbox).Paint();//显示实现接口需要类型转换,调用方法2
}
}
------解决方案--------------------
显示实现接口是指在实现接口的成员前加上接口的全名,此成员不能有任何访问修饰符,也不能是静态的。
该成员在实例中不可见,必须通过接口访问时方可见。

主要是解决接口方法与现存方法同名的问题。
------解决方案--------------------

------解决方案--------------------
/// <summary>
/// 接口的定义
/// </summary>
public interface IA
{
void a();
}
/// <summary>
/// 显式的实现
/// </summary>
public class A : IA
{
public void a()
{
throw new Exception( "The method or operation is not implemented. ");
}
}
/// <summary>
/// 隐式的实现
/// </summary>
public class B : IA
{
void IA.a()
{
throw new Exception( "The method or operation is not implemented. ");
}
}


写反了……
------解决方案--------------------
好了好了
LZ以后这样的问题自己查下MSDN就可以了……
别问了!
------解决方案--------------------
显示接口实现,可以达到对外“隐藏”成员的效果,即“私有化”

因为,你只有显示滴将类型转换为目标接口才可以调用其接口成员

然而,显示接口的初衷是为了,解决,多个被实现的接口成员原型冲突问题