[".NET Framework Design"] Chapter 15 Interface

zhaozj2021-02-16  73

Chapter 15 Interface

Summary:

The application of the interface and the application of the fully qualified name define the application of the interface.

First, interface and inheritance

l C # Support Single Implementation Inheritance and Multi-Interface Inheritance 1 You can define: Event, Non-Non-Non-Non-Enterprise (Properties), Enable (Indexer); C # does not allow interface to define any static members (CLR allows definition of static members); The CLR does not allow interface definition instance fields and constructors. l Default to the Public Abstract method, but not using any modifier to modify (including public) l converting the value type into an interface type (assuming an interface), the value type is packaged as a reference type, to call The interface method.

Principles of implementing interface inheritance or implementation: l There is an IS-A relationship using implementation, there is a CAN-DO relationship using interface inheritance L to implement inheritance can inherit the parent type implementation, due to the implementation of the method in the interface, it must be successful Implementation Method L Taking into account the implementation of each person's implementation of the interface, implementing the implementation of the inheritance of the same part of the function can add a method of parent type to the user without affecting the user implemented by this type, and the user must be new for the interface. Method Add Implementation 2, using interface to change fields in the boxed type

In fact, transformation of the packaged type is actually transformed into an interface type, and then varying the value of the call method. Because the package type to the original value of the original value will not change this new value type, the method of calling this new value type cannot be changed to the value of the box.

Third, realize multiple interfaces with the same method

Use the interface name. The form of method name declares the method of that interface. Become a fully qualified interface name, the method defined is considered a private method, and cannot use the type itself; when the type is converted into an interface type, the fully qualified interface name definition has become a public method. (More strange, it is also useful); uses a fully defined interface name definition from any access modifier.

Fourth, display interface member implementation

Problem: Usually the parameters accepted by the interface are Object, such parameters are not strong, may need to define strong types of security in our custom type interface implementation, but also need to implement "Interface Contract" problem resolution: This effect can be achieved by defining the interface method by the above fully qualified interface name. Advantages: Get strong type support, can do not match problems in the compile period, not to the runtime. At the same time, calling custom methods do not have to make packing operations, improve efficiency. Disadvantages: Since we hide the interface implementation in the type (must expose the transformation to the interface to expose the interface definition), it may be inconvenient for use, so this method should be used with caution.

转载请注明原文地址:https://www.9cbs.com/read-16574.html

New Post(0)