一、显示实现接口和隐式实现接口
1、实现接口过程中若有两个接口中的某成员有相同名字则需用显示实现来解决,显示实现接口的类不能直接调用继承于接口的成员,必须先转换才行。
2、 隐式实现接口,一般情况下就用隐式显示。不用转成接口再调用方法。
public static void Main(string[] args) { //显示实现的 Ipaintpeople people=new Paint (); people.Paint();//此方法是显示实现的接口中的方法,必须转成 Ipaintpeople 才能访问此方法 var people2=new Paint (); people2.Paintpig();//此方法非显示实现的接口中的方法,不用转成 Ipaintpeople 后访问此方法 //隐式实现的 var dog=new Paint2(); dog.Paint(); Console.ReadKey(true); } } public interface Ipaintpeople { void Paint(); } public interface Ipaintdog { void Paint(); } //显示实现接口 public class Paint:Ipaintdog,Ipaintpeople { void Ipaintdog.Paint() { Console.Write("Ipaintdog,Press any key to continue . . . "); } void Ipaintpeople.Paint() { Console.Write("Ipaintpeople,Press any key to continue . . . "); } public void Paintpig() { Console.Write("Paintpig,Press any key to continue . . . "); } } //非显示实现接口 public class Paint2:Ipaintdog { public void Paint() { Console.Write("Ipaintdog,Press any key to continue . . . "); } }