Exhibit the charm of sub-type polymorphism ------ From the viewed point of view (1)

zhaozj2021-02-16  49

Show the charm of child type polymorphism

View polymorphism from the viewed point of view (1)

Summary:

The Java programmer often uses the polymorphism of the object to call the appropriate method in the appropriate place, which is very magical. This method is achieved by inheritance mechanism. However, a rigorous experiment can make it very clear and revealed, understanding the polymorphism as a more appropriate concept, better than the explanation of the inheritance mechanism. This understanding can help programmers better use polymorphism. (3600 words)

------ WM. Paul Roges

"Polymorphism (Polymorphism)" comes from Greek, meaning "multiple forms". Most Java programmers regard polymorphisms as an object of the object, making it calling the correct method version. Despite this, this-oriented point of view leads to a polymorphic magical function, better than thinking of pure concepts.

Polymorphism in Java is always a polymorph of subtypes. Almost mechanically produce some polymorphic behavior, so that we don't consider the types of problems involved. This paper studies a type of object-oriented object-oriented, analyzes how to distinguish the behavior of the object and the behavior of the object to be expanded. The polymorphisms that thrown Java are from inheritance, we can still feel that the interface in Java is a group of object sharing without public code.

Polymorphism

Polymorphisms are a very common concept in object-oriented language. Although we often mix polymorphisms to talk, there are actually four different types of polymorphism. Before you start formal sub-type polymorphism, let's first look at the polymorphism in normal object-oriented.

LUCA Cardelli and Peter Wegner ("On Understanding Types, Data Abstract, And Polymorphism" author, article reference resource link) divide multiple categories: specific and universal ---- Four small classes : Forced, overloaded, parameters and included. Their structure is as follows:

| - Forced

| - - Specific - |

| - Overloaded

Polymorphism - |

| - Parameter

| - General - |

| -

In such a system, the polymorphism exhibits a variety of forms of ability. Universal polymorphisms have a large number of objects of the same structure, they have a common feature. Specific polymorphisms involve objects with small parts without the same feature. Four polymorphisms can be described below:

Mandatory: an implicit method of type conversion.

Overloaded: Use a marker as a plurality of meaning.

Parameter: The same operation is provided for different types of parameters.

Contains: The class contains an abstract operation.

I will briefly introduce these polymorphisms before telling the child type.

Mandatory polymorphism

Forced polymorphism implicit parameters, convert the parameters to the compiler to think that the correct type is to avoid errors. In the following expressions, the compiler must determine the work that the binary operator ' ' should do:

2.0 2.0

2.0 2

2.0 "2"

The first expression adds two Double operands; this usage is specifically declared in Java.

The second expression adds Double type and int. This operation is not clear in Java. However, the compiler implicitly converts the second operand to a Double type and is a Double type addition. It is very convenient for programmers, otherwise a compilation error will be thrown, or forced programmers explicitly convert int to Double.

The third expression adds Double to a String. Such actions are not defined in Java. So, the compiler converts Double to the String type and make them in series.

Mandatory polymorphism will also occur in method calls. Suppose class derived inherits class base, class c has a method, prototype M (BASE), in the following code, the compiler implicitly converts the DeriveD class object DeriveD to the Base class. This implicit conversion makes the M (Base) method all parameters that can be converted to the Base class. C c = new c ();

Derived derived = new derived ();

C.M (Derived);

Also, implicit forced conversion can avoid the trouble of type conversion and reduce compilation errors. Of course, the compiler will still prioritize the type of object that matches the defined.

Overloaded polymorphism

The overload allows the same operator or method to expire. There are two meanings on the above program: two Double types are added; two strings are connected. There is also an integer, long integer, and so on. The overload of these operators depends on the selection made according to the context. The previous compiler will implicit the operand to the type of fully compliant operator. Although Java explicitly supports overload, user-defined operator overload is not supported.

Java supports user-defined functions overload. A class can have the same name, these methods can have different significance. In these overloaded methods, the number of parameters must be satisfied, and the parameter type in the same position is different. These differences can help the compiler distinguish between different versions.

The compiler represents a different method in this unique characteristic, which is more effective than the name. According to this, all polymorphisms can be compiled.

Compulsory and overloaded polymorphisms are classified as specific polymorphism because these polymorphisms are in a specific sense. These areas of being drawn to the programmer have brought great convenience. Forced polymorphism eliminates the type of trouble and compile errors. Heavy duty polymester like a piece of sugar, allowing programmers to express different methods with the same name, very convenient.

Polymorphism of parameters

Parameter polymorphism allows many types to abstract into a single representation. For example, in the List abstraction, a set of objects having the same features are described, providing a universal template. You can reuse this abstract class by specifying a type. These parameters can be any type of user-defined, and a large number of users can use this abstraction class, so the parameter polymorphism is undoubted to become the most powerful polymorphism.

At first glance, the abstraction of the above seems to be the function of java.util.list. However, Java actually does not support the real security type of style parameters, which is also the reason why Java.util.List and Java.util's other collection classes are written with the original java.LANG.Object (refer to my article) "A primordial interface?" To get more details). Java's single inheritance mode solves some problems, but does not exert all the features of parameter polymorphism. Eric Allen has a wonderful article "Behold The Power Of Parametric Polymorphism" describes the need for Java common type and it is recommended to give Sun's Java Specifications. link)

Polymorphism

The type of polymorphic pass value and the collection of the collection implementation of the polymorphism. In many object-oriented languages ​​including Java, the relationship is a subtype. Therefore, Java contains polymorphisms are a plurality of subtypes.

In the early days, the polymorphisms mentioned by Java developers have the polymorphism of the species of the species. Through a type of view, we can see the powerful function of subtype polymorphism. In the following articles, we will carefully explore this issue. For the sake of concise, the polymorphisms in the following refers to the polymorphism.

Type view

The UML class diagram of Figure 1 gives a simple inheritance relationship of classes and types to explain the polymorphism. The model contains five types, 4 classes, and one interface. Although the UML is called a class diagram, I see it as a type. As described in "Thanks Type and Gentle Class," each class and interface are a type of user-defined. According to an independent implementation (such as a type of view), each rectangle in the figure below represents a type. From the implementation method, four types use the structure of the class, an interface to the interface. Figure 1: UML class diagram of the demonstration code

The following code implements the type of data defined for each user, I am very simple to implement.

/ * Base.java * /

Public Class Base

{

Public String M1 ()

{

Return "Base.m1 ()";

}

Public String M2 (String S)

{

Return "Base.m2 (" S ")";

}

}

/ * IType.java * /

Interface ITYPE

{

String M2;

String m3 ();

}

/ * Derived.java * /

Public Class Derived

Extends Base

IMPLEMENTS ITYPE

{

Public String M1 ()

{

Return "Derived.m1 ()";

}

Public String M3 ()

{

Return "Derived.m3 ()";

}

}

/ * Derived2.java * /

Public class deived2

Extends Derived

{

Public String M2 (String S)

{

Return "Derived2.m2 (" S ")";

}

Public String M4 ()

{

Return "derived2.m4 ()";

}

}

/ * Separate.java * /

Public Class Separate

IMPLEMENTS ITYPE

{

Public String M1 ()

{

Return "Separate.m1 ()";

}

Public String M2 (String S)

{

Return "Separate.m2 (" S ")";

}

Public String M3 ()

{

Return "Separate.m3 ()";

}

}

In this type of declaration and class definition, Figure 2 describes Java instructions from the concept's view.

Derived2 derived2 = new deerived2 ();

Figure 2: References on Derived2 objects

The DeriveD2 object is declared above, which is DeriveD2 class. The topmost of Figure 2 describes DeriveD2 to a collection window, although the DeriveD2 object under this is visible. Here is a hole for each DeriveD2 type. Each operation of the DeriveD2 object is mapped to the appropriate code, as described above. For example, the DeriveD2 object maps the M1 () method defined in Derived. Moreover, the M1 () method of the Base class is also overloaded. A delived2 reference variable does not have the right to access the M1 () method in the Base class. But this does not mean that it is not possible to use the method of super.m1 () to use this method. This code is not suitable for the variables of the derived2 reference. Derived2's other operational mappings also indicate the code execution of each type of operation. Since you have a DeriveD2 object, you can use any derived2 type variable to reference it. As shown in Figure 1, Derived, Base and ITYPE are the base class of Derived2. Therefore, it is useful to reference the Base class. Figure 3 depicts the conceptual point of view of the following statements.

Base base = derived2;

Figure 3: Base class references attached to Derived2 objects

Although the reference of the Base class does not have to access M3 () and M4 (), it does not change any feature and operation mapping of its DeriveD2 object. Whether it is the variable Derived2 or BASE, it is the same as the code executed by M1 () or M2 (String).

String TMP;

// derived2 reference (Figure 2)

TMP = derived2.m1 (); // TMP is "derived.m1 ()"

TMP = derived2.m2 ("hello"); // TMP is "Derived2.m2 (Hello)

// Base Reference (Figure 3)

TMP = base.m1 (); // TMP is "derived.m1 ()"

TMP = Base.m2 ("Hello"); // TMP IS "Derived2.m2 (Hello)

Two references call the same behavior because the derived2 object does not know which method is called. Object only knows when to call, it will execute in the order inherited. Such order determines the m1 () method in the DeriveD2 object, and call the M2 (String) method in Derived2. This result depends on the type of object itself, not the type of reference.

Despite this, it doesn't mean that you use Derived2 and Base references to be exactly the same. As shown in Figure 3, Base's reference can only see the operation of the base type. So, although Derived2 has mappings for methods M3 () and M4 (), the variable base cannot access these methods.

String TMP;

// derived2 reference (Figure 2)

TMP = derived2.m3 (); // TMP is "Derived.m3 ()"

TMP = derived2.m4 (); // TMP is "derived2.m4 ()"

// Base Reference (Figure 3)

TMP = Base.m3 (); // compile-time error

TMP = Base.m4 (); // compile-time error

The DeriveD2 object of the runtime maintains the ability to accept the M3 () and M4 () methods. Type restrictions make the base reference cannot call these methods during the compile period. The type of compile period checks like a set of armor, ensuring that the runtime object can only interact with the correct operation. In other words, the type defines the boundaries between the inter-object interactions. Coping the charm of the current type of polymorphism ------ From the viewed point of view (2)

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

New Post(0)