Java learning note 7 - Polymorphismm

xiaoxiao2021-03-06  66

POLYMORPHISM

Polymorphism is also known as dynamic binding "Dynamic Binding", which is bound to "late binding" or run "run-time bingding". It means to determine which method of the binding according to the object when the program is running. Polymorphism is the third basic characteristic of the object-oriented programming language after data abstraction and inheritance.

Binding: Early Binding: Early Binding: Early Binding: When the binding occurs before the program is running (Late Binding): When the program is running, it is determined which method is determined according to the type of object.

Encapsulation creates a new data type by combining data from Characteristics and Behavior. "IMPLEMENTATION HIDING" completes the separation of the interface and implementation by setting the details into private. "Polymorphism" is a "Class" angle to handle this logical separation.

Shape s = new circle (); // Upcasting: Put the Circle object Upcast as shape type s.draw ();

UPCAST ​​is used to use the object's Reference as a base class. (Note: What types of Java know what type of object belongs) Because Derived Class Is A Type Of Base Class, the base class Reference (Shape S) After accepting the object of the Circle, the base class REFERENCE calls are the base class's own method (Late Bind) unless this Method is Late-Bound, it is derived classification (OVERRIDE) this Method The corresponding Method (polymorphism) will be selected according to the object type. Take the above code: s is a Shape type Reference unless Draw () is a dynamically binding method (derived Circle overwritten this DRAW) ()), S.DRAW () will call the Cicle Draw (), otherwise the call is the base class Shape's Method Private and Final Method will use Early-Binding because they cannot be override. (Note: The private method is automatically finaly) It is recommended to name the derived class of Method with the name of the Private Method of the base class. Because this will make people mistaken this Method, actually private is automatically final, and cannot be override.

OVERRIDE indicates a "same" Method in the derived class. It is to rewrite this Method (Note: 1. "The same" means: the same name and the parameter list, and the return value is the same. 2.Method must be non-final, non-private (the private method is automatically final) Overload: except "same "The same name Method other than Method (ie Override).

When you want to manipulate a group of classes through a public interface, you can use an abstract class. Through dynamic binding mechanisms, those derivatives that meet the characteristics will be called. Abstract class {} Abstract class can make Abstract, or non-Abstract. The derived class inherited an Abstract class, then he either implements all Abstract Method, or you turn yourself into Abstract.

//: c07: polyconstructors.java

// Constructors and polymorphism // Don't Product What you might expect.

Import com.bruceeckel.simpletest. *;

Abstract class glyph {

Abstract void Draw ();

Glyph () {

System.out.println ("Glyph () Before Draw ()");

Draw (); // If you have a derived class object, and the dynamic binding method is called in the base class constructor.

// then it will use the version of the derived class.

System.out.println ("glyph () after draw ()");

}

}

Class roundglyph extends glyph {

Private int RADIUS = 1;

Roundglyph (int R) {

RADIUS = R;

System.out.println (

Roundglyph.Roundglyph (), RADIUS = " RADIUS);

}

Void Draw () {

System.out.println (

Roundglyph.draw (), Radius = " RADIUS);

}

}

Public class polyconstructors {

Private static test monitor = new test ();

Public static void main (String [] args) {

New rounceph (5);

Monitor.expect (new string "{

"Glyph () before draw ()",

"Roundglyph.draw (), Radius = 0",

"Glyph () after draw ()",

"ROUNDGLYPH.ROUNDGLYPH (), RADIUS = 5"

});

}

} ///: ~

If you have a derived class object and call the dynamic binding method in the base class constructor, it will use the version of the derived class.

The true initialization process is this: 1. Before performing other work, the memory assigned to this object will be initialized into two-way zero. 2. As mentioned above, the construction function of the base class first. At this time, the overwritten DRAW () method (yes, calling before calling the constructor of the roundglyph), then it discovered that the value of RADIUS is still zero due to the influence of the first step. 3. Data members are initialized in order of their declaration. 4. Call the body of the derived class constructor.

A good constructor should: set the status of the object with the least amount of work, and avoid the calling method as much as possible. The only method of constructor can safely call is the Final / Private method of the base class. They will not be overwritten, so this unexpected behavior will not occur.

Once you understand the polymorphism, you will feel that all things should be inherited, because the polymorphism is too smart. But this will increase the burden of design. In fact, if you think that "To create a new class", you think that you want to use inheritance, things will become more complicated. A preferred approach is to consider synthesis, especially when you don't know which class is inherited. Synthesis does not force you to make the design into a class. In addition, it is more flexible, because when using synthesis, you can dynamically select the type of member (and their behavior), and use inheritance, you have to specify the exact type of the object when compiling. The following programs demonstrate this: //: C07: Transmogrify.java

// Dynamically Changing The Behavior of An Object

// Via Composition (The "State" Design Pattern).

Import com.bruceeckel.simpletest. *;

Abstract class actor {

Public Abstract Void Act ();

}

Class happyactor extends actor {

Public void Act () {

System.out.println ("Happyactor");

}

}

Class Sadactor Extends actor {

Public void Act () {

System.out.println ("Sadactor");

}

}

Class stage {

PRIVATOR Actor = New HappyActor ();

Public void change () {actor = new sadactor ();

Public void PerformPlay () {actor.act ();

}

Public class transmogrify {

Private static test monitor = new test ();

Public static void main (String [] args) {

Stage stage = new stage ();

Stage.PerformPlay ();

Stage.change ();

Stage.PerformPlay ();

Monitor.expect (new string "{

"Happyactor",

Sadactor

});

}

} ///: ~

There is a general guideline "use inheritance to represent the difference in behavior, and the state data is used to represent the status". The above routines also reflect both; two derived classes are used to represent an ACT () method, while the Stage uses synthesis to represent the state change. In this case, the difference in state will result in different behavior.

Downcast: Revert the base class back to the specific derived class type, so that Reference can call the extended interface of the derived class.

Java type passes must be checked! So, although it is only used to use a pair of parentheses to make some ordinary type conversion, the system will check these conversion to ensure that it is indeed the type you want to convert. If not, you will get a ClassCastException. This type of runtime check is called "Run-Time Type Identification Abbreviation is RTTI".

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

New Post(0)