Implicit and Explicit Interface Implementation

 Example 3-1. Defining and implementing an interface

   public interface IMyInterface
    {
       void Method1(  );
       void Method2(  );
      d Method3(  );
    }
     
    public class MyClass : IMyInterface
    {
       public void Method1(  )
       {...}
       public void Method2(  )
       {...}
       public void Method3(  )
       {...}
       //other class members
    }
 

As trivial as Example 3-1 is, it does demonstrate a number of important points. First, interfaces have visibilityan interface can be private to its assembly (using the internal access modifier) or it can be used from outside the assembly (with the public access modifier), as in Example 3-1. Second, even though the methods the interface defines have no access modifiers, they are by definition public, and the implementing class has to declare its interface methods as public. Third, there is no need to use new or override to qualify the method redefinition in the subclass, because an interface method by its very nature can't have any implementation and therefore has nothing to override. (If you aren't familiar with the new or override keywords, see the sidebar "C# Inheritance Directives" later in this chapter.) Finally, the class must implement all the methods the interface defines, without exception. If the class is an abstract class, it can redefine the methods without providing concrete implementation.

即使这些方法没有访问修饰符,默认是public

没有必要使用new或者override来确定方法在子类中被重定义

To interact with an object using an interface, all a client has to do is instantiate a concrete class that supports the interface and assign that object to an interface variable, similar to using any other base type. Using the same definitions as in Example 3-1, the client code might be:

    IMyInterface obj;
    obj = new MyClass(  );
    obj.Method1(  );


Interfaces promote loose coupling between clients and objects. When you use interfaces, there's a level of indirection between the client's code and the object implementing the interface. If the client wasn't responsible for instantiating the object, there is nothing in the client code that pertains to the object hidden behind the interface shield. There can be many possible implementations of the same interface, such as:

接口提供了客户和对象之间的松耦合。当你使用接口时,客户端代码和实现接口的类之间是间接的。如果客户
端不负责实例化一个对像,
    public interface IMyInterface
    {...}
    public class MyClass : IMyInterface
    {...}
    public class MyOtherClass : IMyInterface
    {...}


When a client obtains an interface reference by creating an object of type MyClass, the client is actually saying to .NET "give me MyClass's interpretation of the way IMyInterface should be implemented."

Treating interfaces as binary contracts, which shields clients from changes made to the service providers, is exactly the idea behind COM interfaces, and logically, .NET interfaces have the same semantics as COM interfaces. If you are an experienced COM developer or architect, working with interfaces is probably second nature to you, and you will feel right at home with .NET interfaces.

把接口当作二元契约(协议),向客户隐藏了服务提供者之间的变化,这实际是COM接口的主意

However, unlike COM, .NET doesn't enforce separation of the interface from the implementation. For example, using the definitions in Example 3-1, the client's code can also be:

但是,.net不强迫接口和实现之间的分离。

    MyClass obj;
    obj = new MyClass(  );
    obj.Method1(  );

(考!这也可以!)

Because of the way the server in Example 3-1 implements the interface (as public members), nothing prevents the client from programming directly against the object providing the service, instead of the interface. I believe this is because .NET tries to make component-oriented programming accessible to all developers, including those who have trouble with the more abstract concepts of interface-based programming (see the section ".NET Adherence to Component Principles" in Chapter 1). The fact that something is possible, of course, doesn't mean you should go ahead and do it. Disciplined .NET developers should always enforce the separation, to retain the benefits of interface-based programming.

 

Explicit Interface Implementation

The way of implementing an interface shown in the previous section is called implicit interface implementation, because a public method with a name and signature that match those of an interface method is implicitly assumed to be an implementation of that interface method.

Example 3-3 demonstrates a simple technique that allows server developers to enforce the separation of the interface from the implementation. The server implementing the interface can actually prevent clients from accessing the interface methods directly by using explicit interface implementation. Implementing an interface explicitly means qualifying each interface member name with the name of the interface that defines it.

这个方法允许服务器开发者强制接口和实现的分离。服务器可以阻止客户直接使用(显式接口实现)来访问接口的方法。

这就要求在每个成员函数前使用接口的名字。

Example 3-3. Explicitly implementing an interface
    public interface IMyInterface
    {
       void Method1(  );
       void Method2(  );
    }
    public class MyClass : IMyInterface
    {
       void IMyInterface.Method1(  )
       {...} 
       void IMyInterface.Method2(  )
       {...}
       //Other methods and members
    }


Note that the interface members must be implicitly defined as private at the class's scope; you can't use any explicit access modifiers on them, including private. The only way clients can invoke the methods of explicitly implemented interfaces is by accessing them via the interface:

在类范围内接口成员必须被隐式定义为private,你不能显式使用任何访问修饰符,包括private

显式实现接口,客户调用接口成员的唯一方法是通过接口。

    IMyInterface obj;
    obj = new MyClass(  );
    obj.Method1(  );


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值