先看个例子
声明两个接口
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();
}
}
看到两个接口的实现后可总结:
最后用类的方式调用方法会出错,需要用
as转换接口类型,平常项目的使用一般都是
实现隐式接口,具体要看实际情况设计。
|
【c#】隐式接口和显式接口的区别
最新推荐文章于 2024-07-03 11:44:03 发布