显示实现接口。
顯示實現接口的作用:解決方法重名問題。
下面例子中,Student類同時繼承了兩個接口,但兩個接口有一個共同名稱的方法(有不同的功能),實現接口時只能實現一個,想兩個都實現就用顯示實現接口。
class Program{static void Main(string[] args){Student student = new Student();student.Fly();}}public interface IFace2{void Fly();}public interface IFace1{void Fly();}public class Student : IFace1,IFace2{public void Fly(){Console.WriteLine("IFace1的實現");}}
顯示實現接口:
class Program{static void Main(string[] args){Student student = new Student();student.Fly();IFace2 face2 = new Student();face2.Fly();}}public interface IFace2{void Fly();}public interface IFace1{void Fly();}public class Student : IFace1,IFace2{public void Fly(){Console.WriteLine("IFace1的實現");}void IFace2.Fly(){Console.WriteLine("IFace2的實現");}}
?
顯示接口沒有訪問修飾符,默認私有的,顯示接口實現時是:接口名.方法名
因為顯示實現接口默認是private,所以用接口變量來調用。
轉載于:https://www.cnblogs.com/zhangyuhao/p/10477388.html
總結
- 上一篇: 为什么QQ邮箱登录不了?
- 下一篇: web渗透测试基本步骤