Object-oriented technology first polymorphism (original)

xiaoxiao2021-03-06  56

Object-oriented technology first polymorphism

One, what is more? Why support polymorphism?

Polymorphism is a commonly existent, such as three forms of Water: ice, water, steam, and examples of arithmetic operations 1 1, 1 0.5, 1/2 0.5, etc.

Multi-state uses a classic English to explain: a value can belong to multiple type.

So what is the benefit of supporting polymorphism in software design? First, you can make the mathematical operation in the program conform to the routine mathematical calculation rules, providing a stronger expression ability; second makes the same operational semantics for different types of data, and the reproduction of the program can be reused, and the resources identified, It can improve the readability and understandability of the program.

The premise of software systems support polymorphism is to determine the type in static (compile) or dynamic (runtime).

What are the classifications of two, polymorphism?

Polymorphism is divided into two: universal and specific polymorphisms (AD hoc). The difference between the two is that the former is not limited to the type of work, allowing the same code to perform the same value; the latter only valid for a limited number of types, and different types of values ​​may perform different codes.

GM is divided into parameter polymorphism and containing polymorphism; specific polymorphism is overloaded

OverLoading and Mandatory Polymorphism (COERCION). Specifically, see the following figure:

2.1 parameter polymorphism

Concept: Use parameterization templates to make a variety of types by giving different types of parameters.

For example: Generic (class) in the ADA.

See here:

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z2.jpg

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z3.jpg

Analysis: These two packages:

· Application connotation is consistent: provided are all stack abstract operations.

· Implementation structure is similar: use the same type of data structure to achieve the same code.

· The type of element is different, and some details expressed in constants are different.

Comparison: The function is a meaning of similar statement sequences, which is clearly abstract, indicating the same part in the function, and uses the shape to refer to the different parts, and use the factor to reflect different parts. However, the parameters of the function can only be data and cannot be type.

Class belongs to a variable type, function, constant, data value in a parametric form, and compile time

(Static) is instantiated, the result is a specific structure (type, function, etc.). And the type of instantiation can be static

The line can also be done, but the result is a value.

The polymorphic example of the generic structure in Ada is as follows:

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z4.jpg

When the generic structure is called:

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z5.jpg

visible:

· Do not limit the type obtained by the actual parameters.

· Execute the same code for different arctors. Therefore, a polymorphism is constituted.

2.2 Contains Polymorphism

Concept: The same operation can be used for a type and its subtype. (Note is a subtype, not a subclass.) Contains polymorphism

Types of time you need to run. Such as the suborder in Pascal.

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z6.jpg

A few things to pay attention to:

1. Anti-morNotonic is included in the form of a polymorphic operation. That is, the operation on the type T, when the definition domain is reduced to a subtype of T, its value domain should not be less than T.

2.3 overload polymorphism

Concept: The same name (operator, function name) has different types in different contexts. Most operators in the basic type of programming language are overloaded. Such as C language

==: int * INT -> INT

==: double * double-> int

==: char * char-> int

There are some programming languages ​​that allow users to customize overloaded operators. Such as C languages:

Bool Operator == (Complex1 C1, Complex C2);

Bool Operator == (Address A1, Address A2);

Overload polymorphic operators or function names, what he corresponds to a different implementation.

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z7.jpg

2.4 forced polymorphism

Concept: The compiler is operated by semantic operations to transform the type of operation objects to comply with functions or operators. Most operators of the basic type in the programming language, when different types of data are mixed,

Compilation programs generally make mandatory polymorphism. Programmers can also display mandatory polymorphic operations (CASTING).

E.g:

http://blog.9cbs.net/images/blog_9cbs_net/upwaker/45928/o_z8.jpg

It should be noted that it is not an all-in-one types. Implementing forced polymorphisms between different types, usually requires different conversion operations. The principle of mandatory polymorphism is that the type of value set is smaller (ie, the amount of storage space is small), and the transformation set set contains the type of the former (ie, the occupied storage space), which, should pay attention to the possible value Damage (especially when using Casting).

Sometimes, forced polymorphism and overload polymorphism are mixed. For example, for expressions 1 2; 1.0 2; 1 2.0; 1.0 2.0;

There will be a variety of explanations:

· Operators four overload polymorphisms;

· Operators only one: double * double -> Double, to enhance the integer to participate in the operation into floating point numbers;

· Operator There are two overload polymorphisms: int * int-> int and double * double -> Double, to convert integer in the hybrid into floating point numbers.

Three, other types of polymorphism

In most literatures, there are two polymorphics in object-oriented programming languages:

· Polymorphisms existing between classes with inheritance relationships (but not completely equivalent to inclusion);

• A dynamic binding mechanism determines the object type of the message (such as the virtual function in the C language) accepted at runtime. Full text

Zhang Xiujun

2004. 9 .17

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

New Post(0)