先看个例子
声明两个接口
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public interface IFg { int Add(); void Fei(); } public interface IWf { int Add(); void Fei(); }
Fg类开始继承隐式接口
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public class Fg : IFg, IWf { public int Add() { throw new NotImplementedException(); } public void Fei() { throw new NotImplementedException(); } }
Fg类开始继承显示接口
复制代码
看到两个接口的实现后可总结:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public 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转换接口类型,平常项目的使用一般都是实现隐式接口,具体要看实际情况设计。
最后
以上就是单薄煎蛋最近收集整理的关于C# 隐式接口和显示接口的区别的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复