Object-Oriented Technology lecture 1 Polymorphism

Source: Internet
Author: User

Object-Oriented Technology lecture 1 Polymorphism

1. What is polymorphism? Why is polymorphism supported?

Polymorphism is a common phenomenon, such as the three forms of water: Ice, water, steam, and arithmetic operations 1 + 1, 1 + 0.5, 1/2 + 0.5.

Polymorphism is interpreted as a value can belong to multiple types.

So what are the benefits of supporting polymorphism in software design? First, it can make the mathematical operations in the program conform to the regular mathematical operation rules and provide a stronger expression capability for the program. Second, it provides the same operation semantics for different types of data, it can realize reuse of programs, and reuse of identified resources can improve the readability and comprehension of programs.

The premise that the software system supports polymorphism is to be able to determine the type statically (during compilation) or dynamically (during runtime.

2. What are the types of polymorphism?

There are two types of polymorphism: General polymorphism (universal) and specific polymorphism (ad hoc ). The difference between the two is that the former does not limit the type of work and allows the same code to be executed for different types of values. The latter is only valid for a limited number of types, in addition, different types of values may need to be executed.

Common polymorphism can be divided into parametric and inclusion sion. Specific polymorphism can be divided into multiple types: overload.

State (overloading) and forced polymorphism (Coercion ). For details, see:

2.1 parameter Polymorphism

Concept: A parameterized template is used to give different type parameters so that a structure has multiple types.

For example, generic (class) in Ada ).

See the following example:

Analysis: for the two packages:

· Consistent application connotation: Provides abstract stack operations.

· Similar implementation structure: similar data structures are used to achieve the same code.

· The types of elements are different, and some details expressed using constants are different.

Comparison: a function is a clear abstraction of similar statement sequences. It uses statements in a function to represent the same part, uses form parameters to specify different parts, and uses real parameters to reflect different parts. However, function parameters can only be data, not type.

Class can represent variable types, functions, constants, and data values in the form of parameters in an abstract structure. during compilation

(Static) is instantiated, and the result is a specific structure (type, function, etc ). In addition, the type instantiation can be static.

Row, but the result is a value.

Examples of polymorphism in the generic structure in ADA are as follows:

When calling the generic structure:

Visible:

· There is no restriction on the type of the real parameter.

· The same code is executed for different real parameters.

As a result, polymorphism is formed.

2.2 inclusion Polymorphism

Concept: the same operation can be used for a type and its subtypes. (Note that it is a child type, not a subclass .) Inclusion Polymorphism

Generally, you need to check the type during running. For example, subboundaries in Pascal.

Notes:

1. Operations containing polymorphism have anti-mornotonic ). That is, the operation on a type T. When its defined domain is reduced to a sub-type of T, its value range should be no less than T.

2.3 overload Polymorphism

Concept: the same name (operator, function name) has different types in different contexts. In programming languages, most of the basic types of operators are overloaded. For example

==: Int * int-> int

==: Double * double-> int

==: Char * char-> int

Some programming languages allow users to customize overload polymorphism operators. For example, in C ++:

Bool operator = (complex1 C1, complex C2 );

Bool operator = (address A1, address A2 );

Overload polymorphism operator or function name, which usually corresponds to different implementations.

2.4 forced Polymorphism

Concept: the compiler forcibly transforms the type of the operation object through semantic operations to meet the requirements of functions or operators. In programming languages, most operators of the basic type are used to perform hybrid operations on different types of data,

Compilation programs generally perform forced polymorphism. Programmers can also perform forced polymorphism (casting) explicitly ).

For example:

Note that no forced polymorphism can be performed between any two types. To implement forced polymorphism between different types, you usually need to perform different conversion operations. The principle of forced polymorphism is to convert a type with a smaller value set (that is, a smaller footprint) into a value set that contains the former (that is, a larger footprint) type, otherwise, pay attention to possible damage to the value (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 are multiple interpretations of the polymorphism:

· Operator + there are four types of overload polymorphism;

· Operator + only one type: Double * double-> double, which must be forcibly converted into a floating point number;

· The operator + has two types of overload polymorphism: int * int-> int and double * double-> double. The integer in the hybrid operation must be forcibly converted to a floating point number.

 
Iii. polymorphism of other types

Currently, most documents hold that there are two types of polymorphism in the object-oriented programming language:

· Polymorphism between classes with inheritance relationships (but not completely equivalent to inclusion polymorphism );

· The dynamic binding mechanism is used to determine the type of objects that receive messages (such as virtual functions in C ++) at runtime ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.