先看个例子
声明两个接口
public interface IFg
{
int Add();
void Fei();
}
public interface IWf
{
int Add();
void Fei();
}
Fg类开始继承隐式接口
public class Fg : IFg, IWf
{
public int Add()
{
throw new NotImplementedException();
}
public void Fei()
{
throw new NotImplementedException();
}
}
Fg类开始继承显示接口
public class Fg : IFg, IWf
{
int IWf.Add()
{
throw new NotImplementedException();
}
int IFg.Add()
{
throw new NotImplementedException();
}
void IWf.Fei()
{
throw new NotImplementedException();
}
void IFg.Fei()
{
throw new NotImplementedException();
}
}
看到两个接口的实现后可总结:
第一、显示接口中的方法没有修饰符二隐式接口方法的修饰符为public
第二、显示接口中的方法可以看到从哪里来【通过接口访问,避免访问歧义】,来源相当清晰,隐式接口看不出来源
第三、显示接口会把父级接口中的方法和属性完全继承,隐式接口会过滤冗余的方法
最后用类的方式调用方法会出错,需要用as转换接口类型,平常项目的使用一般都是实现隐式接口,具体要看实际情况设计。