3、显性和隐性接口实现
这很重要吗?是的,非常重要,你知道它们之间的语法差异吗?其实它们存在根本性的区别。类上的隐性接口实现默认是一个公共方法,在类的对象或接口上都可以访问。而类上的显性接口实现默认是一个私有方法,只能通过接口访问,不能通过类的对象访问。下面是示例代码:
- <CODE>
- INTERFACE public interface IMyInterface
- { void MyMethod(string myString);
- }
- CLASS THAT IMPLEMENTS THE INTERFACE IMPLICITLY public MyImplicitClass: IMyInterface
- { public void MyMethod(string myString)
- { ///
- } }
- CLASS THAT IMPLEMENTS THE INTERFACE EXPLICITLY
- public MyExplicitClass: IMyInterface {
- void IMyInterface.MyMethod(string myString) {
- /// }
- }
- MyImplicitClass instance would work with either the class or the Interface: MyImplicitClass myObject = new MyImplicitClass();
- myObject.MyMethod(""); IMyInterface myObject = new MyImplicitClass();
- myObject.MyMethod("");
- MyExplicitClass would work only with the interface: //The following line would not work.
- MyExplicitClass myObject = new MyExplicitClass(); myObject.MyMethod("");
- //This will work IMyInterface myObject = new MyExplicitClass();
- myObject.MyMethod("");










