In-depth analysis of C #'s polymorphism

xiaoxiao2021-03-06  125

Author:

Tianyuyi, what is multi-state

Another important concept in object-oriented programming is polymorphism. At runtime, you can call the method in which the derivative class can be invoked by pointing to the base class. A set of objects can be placed in an array, then call them, in which case the polymorphism is embodied, these objects do not have to be the same type of object. Of course, if they all inherit from a certain class, you can put these derived classes in an array. If these objects have the same name method, you can call the same name method for each object.

The same operation acts on different objects, which can have different interpretations, producing different execution results, which is polymorphism. Polymorphism is achieved by sending a virtual function type method in the base class.

In an object-oriented system, polymorphism is a very important concept that allows customers to operate an object, by the object to complete a series of actions, which is implemented which action, how to implement the system is responsible.

The term "polymorphism" was earlier for biology, refers to the organism of the same race has the same characteristics. In C #, the definition of polymorphism is that the same operation acts on different classes, and different classes will perform different interpretations, and finally produce different execution results. C # supports two types of polymorphisms:

● Polymorphism when compiling

The polymorphism of compile is achieved by overloading. For non-virtual members, the system determines what operations such as information such as the transmission of the parameters and the type returned when compiling.

● Polymorphism at runtime

The multi-state of the runtime refers to what operations implementation according to the actual situation when the system is running. In C #, the polymorphism at runtime is implemented by virtual members.

The polymorphism of compile provides us with fast running speed, while the running polymorphism brings highly flexible and abstract features.

Second, realize polymorphism

Multi-state is class as a method (these methods are called in the same name) to provide different implementations. The polymorphism allows a method of calling a certain method without considering the particular implementation provided by the method. For example, there may be a class named Road, which calls another type of DRIVE method. This other class Car may be Sportscar or Smallcar, but both provide Drive methods. Although the implementation of the Drive method varies depending on the class, the ROAD class can still call it, and the results it provide can be used and explained by the road class.

You can use different ways to implement the polymorphism in the component:

● Interface polymorphism.

● Inheritance polymorphism.

● Polymorphisms achieved by abstract classes.

Interface polymorphism

Multiple classes enable the same "interface", while a single class can implement one or more interfaces. The interface is essentially a definition of how the class needs to be responded. Interface Description Categories require implementation methods, properties, and events, and parameter types that each member needs to receive and returned, but leave the specific implementation of these members to implement class.

One of the powerful technology in component programming is capable of implementing multiple interfaces on an object. Each interface consists of a small part of the method, attribute, and events. By implementing an interface, the component can provide a function of any other component requiring the interface without considering the specific functions included. This makes the release of the subsequent components to contain different features without interfere with the core function. The most commonly used components of other developers are naturally a member of the component class itself. However, it may be more difficult to use components that contain a large number of members. It can be considered to decompose certain functions of the components as a separate interface of private implementation.

Another benefit of defining the function based on the interface is that the function can be added to the component by defining and implementing additional interface increments. Advantages include:

1. Simplify the design process because the components can be small, with minimal functions; after the component continues to provide minimal features, and continuously insert additional features and determine the appropriate features by actually use those features.

2. Simplify compatibility maintenance because the new version of the component can continue to provide an existing interface while adding a new interface. The subsequent versions of the client application can take advantage of these interfaces. Polymorphism achieved by inheritance

Multiple classes can "inherit" from a single base class. By inheritance, all methods, properties, and events of the base class are received in the same implementation of the class class. In this way, additional members can be implemented as needed, and the base member can be rewritten to provide different implementations. Please note that inheritance classes can also implement interfaces, both technologies are not mutually exclusive.

C # provides polymorphism by inheritance. For small-scale development tasks, this is a powerful mechanism, but for large-scale systems, there is usually a problem. Over-emphasis on inheritance-driven polymorphisms generally lead to large-scale transfer from coding to design, which is not helpful for shortening the total development time.

When is it inheriting a polymorphism? Use inheritance first is to add functions to the existing base class. If the programmer's work efficiency will be greatly improved from the fully debug-based base class frame, the method can increase to the base class without interruption. When the application design contains multiple related classes, these related classes must share the same implementation for some common functions, and you may wish to use inheritance. The overlapping function can be implemented in the base class. The classes used in the application can be derived from the base class. Abstract class consolidation inheritance and implementation, which may be useful when elements that need one of them.

Polymorphisms achieved by abstract classes

Abstract categories also provide elements for inheritance and interface. The abstract class itself cannot be instantiated, it must be inherited. Some or all members of this class may not be implemented, and this implementation is provided by inheritance class. The implemented members can still be rewritten, and the inheritance class can still achieve additional interfaces or other functions.

Abstract classes provide inheritance and interface implementation. Abstract classes cannot be sampled and must be implemented in inheritance classes. It can contain implemented methods and properties, but can also include unrealized processes, which must be implemented in inheritance classes. This allows you to provide invariant grade features in some methods of classes, while maintaining flexibility options for other processes. Another advantage of an abstract class is that when the new version of the component is required, the additional method can be added to the base class as needed, but the interface must remain unchanged.

When is the abstract class? When a set of related components are required to include a group of methods having the same function, but also requires the abstract class when it is flexible in other method implementations. When it is expected that the version problem may occur, the abstraction class also has values ​​because the base class is flexible and easy to modify.

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

New Post(0)