In-depth analysis of C #'s polymorphism (pick)

xiaoxiao2021-03-06  115

In-depth analysis of C #'s polymorphism

Tianyu 2003-3-7 15:01:12

First, what is another important concept in multi-state-oriented programming design 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 polymorphism: ● The polymorphism when compiling time is achieved by overload. 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. ● The polymorphism of the runtime is the operation of the implementation of the actual situation when the multi-state runtime is until 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, implementing polymorphisms is a class as a method (these methods are called in the same name). 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. Polymorphisms in components can be implemented in different ways: ● Interface polymorphism. ● Inheritance polymorphism. ● Polymorphisms achieved by abstract classes. Interface polymorphisms can implement 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. The advantages include: 1. Simplify the design process, because the components can be small, have the smallest function; then, the components continue to provide minimal features, and constantly 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.

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

New Post(0)