Good Java programmers to learn as much as state routes share three characteristics

Good Java programmers to learn as much as state routes share three characteristics, First, what is polymorphic

1. Object-oriented three properties: encapsulation, inheritance, polymorphism. From a certain point of view, almost all of encapsulation and inheritance of polymorphic prepared. This is our final concept, the most important points.

2. Definition of polymorphic: refers to allow objects of different classes respond to the same message. I.e. the same message can be transmitted according to different objects and many different behavior. (Sending a message that is a function call)

3. A multi-state technique called: dynamic binding (dynamic binding), refers to the actual type is determined during execution of the referenced object, which calls the appropriate method for the actual type.

4. The role of polymorphism: decouple the relationship between type.

5. In reality, the polymorphic on numerous examples. For example, press the F1 key to this action, if the current pop-up interface in Flash AS 3 of the document is to help; if the current pop-up in the Word is the Word Help; in the pop-up Windows is Windows Help and Support. The same event on different objects will produce different results.

Three necessary conditions occurring polymorphisms

1, there must be inherited;

2, must be rewritten;

3, references to the parent class subclasses the object.

Second, the multi-state benefits:

Of 1. Alternatively (substitutability). Polymorphic having an alternative to the existing code. For example, polymorphisms of the Circle class work circle, any other circular geometry, such as rings, also work.

2. scalability (extensibility). Polymorphism is scalable code. Add a new sub-class does not affect the existing class of polymorphism, inheritance, and other operational and operational characteristics. In fact, new subclasses plus easier access to polymorphic functions. For example, to achieve a conical, hemispherical half-cones and on the basis of the polymorphism, it is easy to add class polymorphism sphere.

3 of the interface (interface-ability). Polymorphism is the superclass method by signature, provides a common interface to the subclass to subclass or perfected by covering it achieved. Shown in Figure 8.3. FIG Super Shape class implements the interface provides two methods of polymorphism, computeArea () and computeVolume (). Subclasses, such as Circle and Sphere To achieve polymorphism, or improve the coverage of these two interface methods.

4. Flexibility (flexibility). It reflects the flexible operation in applications to improve the efficiency.

5. Simplicity (simplicity). Polymorphic simplify coding and application software modification process, especially in the processing of arithmetic operations and a large number of objects, and especially important to this feature.

Java implementation of polymorphic: interface, override inherited superclass method, a method of overloading the same class.


Guess you like

Origin blog.51cto.com/14479068/2429845