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

zhaozj2021-02-16  54

Show the charm of child type polymorphism

---- Polymorphism from the viewed point of view (2)

Polymorphism

The consistency of the type is the core of polymorphism. Each of the objects, static type inspector must confirm that such an attachment and its objects are consistent. When a reference is successfully attached to another different object, interesting polymorphism is generated. (Strictly speaking, the object type refers to the definition of class.) You can also attach several different references to the same object. Before starting more interesting scenes, let's take a look at why the following situation will not produce a polymorphism.

Multiple references are attached to an object

The examples described in Figures 2 and 3 are attached to an object to two or more references. Although the Derived2 object retains the type of variable after being associated, the reference to the base type in Figure 3 is attached, and its function is reduced. The conclusion is obvious: the reference to a base class is attached to the object of derived class will reduce its capabilities.

How can a development that chooses to reduce the subject's ability? This option is indirect. Suppose there is an object that is called REF is attached to a class containing the following method:

Public String Poly1 (Base Base)

{

Return Base.m1 ();

}

Calling Poly (BASE) with a DeriveD2 is the parameter type check:

Ref.Poly1 (Derived2);

Method Calling A local base type variable is attached to an introduced object. So, although this method only accepts the parameters of the base type, the derived2 object is still allowed. Development This does not have to choose a function of the loss. From the human eye, the attachment of the Base type reference leads to the loss of functionality. However, from the viewpoint of execution, each incoming Poly1 (BASE) is considered to be the object of Base. The execution machine does not care about there is a reference to the same object, which only focuses on the reference to the reference to another object. The type of these objects is inconsistent is not the main problem. The actuator only cares about the object to find the appropriate implementation. Type-oriented views show a huge power of polymorphism.

Reference to multiple objects

Let's take a look at the polymorphic behavior that happened in Poly1 (Base). The following code creates three objects and passed by reference to Poly1 (Base):

Derived2 derived2 = new deerived2 ();

Derived derived = new derived ();

Base base = new base ();

String TMP;

TMP = Ref.Poly1 (Derived2); // TMP IS "Derived.m1 ()"

TMP = Ref.Poly1 (Derived); // TMP IS "Derived.m1 ()"

TMP = Ref.Poly1 (Base); // TMP IS "Base.m1 ()"

The implementation code of POLY1 (BASE) is a M1 () method that calls the passing parameters. Figures 3 and 4 illustrate the system-oriented architecture for the three classes to transmit three classes.

Figure 4: Point Base reference to Derived class, and base object

Please note the mapping of methods M1 () in each figure. In Figure 3, m1 () calls the code of the Derived class; the comments in the above code indicate that PLOY1 (BASE) calls DeriveD.m1 (). Figure 4 The DeriveD object call is still the M1 () method of the DeriveD class. Finally, in Figure 4, the M1 () of the Base object call is the code defined in the base class.

What is the charm of polymorphism? Let's take a look at the Poly1 (Base) code that accepts any parameters belonging to the base class. However, when he received a DeriveD2 object, it actually called the method of DeriveD version. When you derive other classes based on the base class, such as Derived, Derived2, Poly1 (BASE) can accept these parameters, and make the option to call the appropriate method. Polymorphism allows you to extend its use after completing POLY1 (BASE). This seems to be very magical. Basic understanding shows the principle of the polymorphic internal work. In the type of view, the code implemented by the underlying object is non-substantial. Importantly, the Type Checker Selects the appropriate code for each reference during compilation. Polymorphisms make developers to use the type of view, regardless of the details of the implementation. This helps separate the type and implementation (the actual use of the interface and the implementation)

Object interface

Polymorphism depends on the type and implementation of separation, mostly to separate the interface and implementation. But the following points seem to be very confused with the keywords of Java.

More important make developers understand phrase "the interface to an object", typically, according to context, this phrase means that the method defined in all object classes, to all objects disclosed. This tendency to achieve a central point of view, making us more focusing on the ability of the object in operation. In Figure 3, the object surface of the reference panel is marked as "derived2 object". This panel lists all available methods for DeriveD2 objects. But to understand the polymorphism, we must liberate this level, and pay attention to the panel labeled "Base Reference" in a type of perspective. In this point, the type of reference variable indicates the surface of an object. This is just a surface, not an interface. Under the principle of the same type, we can use the object-oriented viewpoint to attach multiple references to an object. The understanding of the phrase of Interface to an Object is not determined.

In the type concept, The interface to an Object Refers references the maximum possible view of the type of view ---- as shown in Figure 2. Point a reference to the same object to narrow the same object - as shown in Figure 3. The type concept can make people achieve the interaction between the objects to achieve detail. The type-oriented viewpoint is more encouraged to use an object to be referenced more than an object's interface. The reference type specifies the interaction between objects. When you think about an object, just understand his type, don't need to consider his implementation details.

Java interface

The polymorphisms discussed above have used the sub-type relationship established by the inheritance relationship of the class. The Java interface also supports user-defined types, relatively, Java interface mechanisms launched a polymorphic behavior on the type hierarchy. Assume a reference variable called REF and pointing it to a class object that contains a method:

Public String Poly2 (ITYPE ITYPE)

{

Return ITYPE.M3 ();

}

In order to understand the polymorphism in POLY2 (ITYPE), the following code creates two objects from different classes and pass them to Poly2 (ITYPE):

Derived2 derived2 = new deerived2 ();

Separate Separate = new separate ();

String TMP;

TMP = Ref.Poly2 (Derived2); // TMP IS "Derived.m3 ()"

TMP = Ref.Poly2; // TMP IS "Separate.m3 ()" The above code is similar to the polymorphism in POLY1 (BASE). The implementation code of Poly2 (ITYPE) is a local version of the local version of each object. As previously, the annotation of the code indicates the result of each call returned CSTRING type. Figure 5 shows the conceptual structure of calling Poly2 (ITYPE) twice:

Figure 5: ITYPE reference to Derived2 and Separate objects

Methods Poly1 (Base) and Poly2 (ITYPE) can be seen directly from the perspective. I will see the skills of these two codes in achieving a layer of understanding of our understanding. The reference to the base class points to a class that is passed as a parameter, and the method of calling the object in accordance with the type of limit. Quote neither knows which code doesn't care. Sub-type relationship during compilation guarantees that the object has the ability to select the appropriate implementation code when being called.

However, they have an important difference in achieving the layer. In the example of Poly1 (BASE) (Fig. 3 and Figure 4), the class-derived-deerived2 class inheritance provides a condition for the establishment of sub-type relationships, and determines which code to call. In the example of Poly2 (ITYPE) (Figure 5), it is completely different dynamic. Derived2 and Separate do not share any hierarchy, but they still show a polymorphic behavior through ITYPE references.

Such a polymorphism is significant in the significance of the function of Java's interface. The UML class diagram in Figure 1 illustrates that Derived is a subtype of Base and ITYPE. Java implements multi-type inheritance by completely detaching, Java implements multi-type inheritance, and there is no annoying issue brought about by Java. A class that is fully detached from the level can be packet in accordance with the Java interface. In Figure 1, interface ITYPE and DERIVED, SEPARATE, and other subtypes of this type should be drawn.

According to this completely different classification method, Java's interface mechanism is a polymorphism that is very convenient, even if there is no way to achieve or rewmbola. As shown in Figure 5, an ITYPE is referenced, and the M3 () method of Derived2 and Separate objects is accessed with polymorphism.

Discuss the interface of the object again

Note the mapping method of the Derived2 and Separate objects in Fig. 5. As mentioned earlier, each of the interfaces of each subject contains M1 (). However, there is no way to use these two objects to show the method M1 () to exhibit a polymorphism. Each object occupies a M1 () method is not enough. There must be a type that can operate the M1 () method, and the object can be seen by this type. These objects seem to have shared the M1 () method, but the polymorphism is impossible without the common base class. Through the interface of the object, it will make this concept to mix this concept.

in conclusion

Sub-type polymorphisms established from the object-oriented polymorphism described in full text, you can clearly recognize this type of view. If you want to understand the idea of ​​sub-type polymorphism, you should transfer your attention from the implementation details to the type. Types divide the object into groups and manage interfaces of these objects. The inheritance hierarchy of the type determines the type relationship required to realize the polymorphism.

Interestingly, the implementation of the implementation does not affect the hierarchical structure of the child type. The type determines what method calls for object calls, and implementation determines how objects do this method. That is, the type indicates responsibility and is responsible for implementation. After the implementation and type separation, we seem to see that these two parts are dancing together, the type determines the name of his dance partners and dance, and the implementation is a designer of dance action.

About the Author:

WM.PAUL ROGERS is a senior engineer and application developer working in Lutris Technologies. He uses open source Java / XML application servers ENHYDRA to propose solutions. He started using Java when he studied marine biology in Mbari (Montery Bay Aquarium Research Institute). At that time, he was responsible for studying how to use new technologies to study marine science. PAUL uses object-oriented technology has been more than nine. references:

"On Understanding Types, Data Abstraction, and Polymorphism," Luca Cardelli and Peter Wegner from Computing Surveys, (December, 1985) - an academic treatise of three important object-oriented concepts: http://research.microsoft.com/Users /LUCA/Papers/onunderstanding.pdf

"Behold the Power of Parametric Polymorphism," Eric Allen (JavaWorld, February 2000) - an excellent overview of the need for introducing generic types to the Java language: http://www.javaworld.com/jw-02-2000/ JW-02-jsr.html

"Add Generic Types to the Java Programming Language," (Java Community Process Program, JSR # 000014) - the Java Specification Request regarding extending the Java language to incorporate parametric polymorphism: http://java.sun.com/aboutJava/communityprocess /jsr/jsr_014_gener.htmlread more from wm. Paul Rogers:

"Thanks Type and Gentle Class" (JavaWorld, January 19, 2001) Explores The Importance of Sepage The Object-Oriented Concepts of Type and Class.

"A primordial interface?" (JavaWorld, March 9, 2001) Uses a type-oriented Perspective to Explore The Implicit Existence of a primordial interface in java.

Wm. Paul Rogers Comodes The Java Beginner Discussion. Ask Him Your Beginner-Level Questions Here: http://www.itworld.com/jump/jw-0413-PolyMorph/forums.itworld.com/webx?230@@.ee6b804 ! Skip = 2899

Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld: http://www.idg.net/jw-subscribeBrowse JavaWorld's Topical Index: http://www.javaworld.com/javaworld/topicalindex /jw-ti-index.html

Limited level, please advise

Email: rdqjuven@hotmail.com

QQ: 1735739

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

New Post(0)