Java Tour (5) Polymorphism

xiaoxiao2021-03-06  23

The polymorphism is the third feature of the object-oriented language after data abstraction and inheritance.

Binding (looks like a sound): Connect the method's call to the method itself is called binding, and when the binding occurs before the program is running, it is called pre-Binding, and When the program is run, according to the type of object, determine the binding method, also called run-time binding, or dynamic binding; all methods of Java are bound to bind That is to say, in general,

You don't have to consider whether it is bound to be bound, all this is automatic.

There is a classic example of "shape", which can be vividly explained what is the binding.

In this example, the base class is a Shape class, which has several derivatives: Circle, Square, Triangle,

Shape s = new circle ();

Here first created a Circle object, then gave it a shape, it seems to be a bit wrong, but it is good, because Circle is indeed a shape, then assume that you call a base class method.

s.draw ();

Maybe you will think that this call should be the shape DRAW method, but it is called Draw (), which is because the reasons for the retrofit. The specific implementation method is that the base class defines a shared interface - that is, all shapes have a Draw () method and an ERASE () method, derived classes override these two methods to provide different behaviors. Here I want to write directly to:

Circle s = new circle ();

s.draw ();

Later, I saw that the author changed this example into a randomly created a Circle, Square, Triangle object, because it still didn't know what the created object is specifically, so it is only possible to use dynamic binding to implement.

From this we see the greatest advantage of polymorphism: scalability. We can add any new type as needed without having to worry about modifying the method in the base class, so in a well-designed OOP program, most methods will be the same as the Draw () method, only to the base class interface. This procedure is scalable because you can add new features through the "New Data Type Inherit Base Class" method. And those who deal with the base interface interface, you can adapt to new classes without making modifications.

For programmers, polymorphism is a very important technology that allows you to "will change and don't change."

Abstracts and abstract methods: To create a class object like a Shape class is not practical, let alone you may still stop the user, so we can use an abstract method to solve this problem. Shaped like:

Abstract void f ();

The class containing one or more abstract methods is an abstract class (containing abstract methods must be defined), the effect of the abstract class is to control a set of classes through a public interface. Its method is like the method of the base class in the above example, just like the goods. And if you create an abstract class object, the compiler will report an error.

If you inherit the abstract class, and intend to create the object of this class, you must implement all the methods defined by the base class, otherwise there is an abstract method exists, then the class is still an abstract class.

Creating an abstract class that does not contain abstract methods can be used to "do not have to create abstract methods, but to ban others from creating this class object."

The constructor is always different, which is involved in the polymorphism. First study an example, review the call sequence of the constructor. ///

Class meal {

MEAL () {system.out.println ("meal ()");}

}

Class Bread {

Bread () {system.out.println ("BREAD ()");}

}

Class cheese {

Cheese () {system.out.println ("cheese ()");}

}

Class lettuce {

Lettuce () {system.out.println ("Lettuce ()");}

}

Class Lunch Extends MEAL {

Lunch () {system.out.println ("Lunch ()");}

}

Class portablelunch eXtends lunch {

Portableelunch () {system.out.println ("PortableLunch ()");}

}

Public class

Sandwich

Extends portablelunch {

PRIVATE BREAD B = New Bread ();

PRIVATE CHEESE C = New Cheese ();

PRIVATETUCE L = new lettuce ();

public

Sandwich

() {

System.out.println ("

Sandwich

() ");

}

Public static void main (String [] args) {

New

Sandwich

();

System.out.println ("correct output:");

System.out.println (

"Meal () / n"

Lunch () / N "

"PortableLunch () / N"

"BREAD () / N"

"Cheese () / N"

"Lettuce () / N"

"

Sandwich

() "

);

}

}

///

In other words, the call sequence of the constructor of the complex object is:

1. Call the constructor of the base class. This is a recursive process, so the root of the inheritance system will be created first, then the next level is derived, with a secondary, to the last inherited structure function.

2, the member object is initialized in accordance with the sequence of objects thereof.

3. Perform the text of the constructor of the inheritance class.

About cleanup work, although it is not used, it is very careful to work.

A good constructor should, "use the least amount of work to set the status of the object, and avoid the way to call the method" Constructor unique method of securely call the basic method of the base class. (This also applies to Private because it is automatically final) they will not overwrite, so this unexpected behavior will not occur.

1> After understanding some concepts of polymorphism, let's take a look at the design of the class in the inheritance mode of the class, "pure inheritance" is like the picture below.

Because of the same interface, the base class can accept any messages sent to the derived class. What you have to do, just upload the object of the derived class, then you will no longer need to know what type of this object is. All objects are handled by polymorphism. It seems to be a good way, but the fact is that your derived class will have more interfaces or methods than the base class, such as the following. It seems that this is the case that is often used in actual, but it also brings a shortcomings that cannot pass the base class to access the extension method. If such an error occurs in the program, the system will throw a ClassCastException. The exception, this runtime type check is called "Runtime Type Identification Run-Time Type Identification (RTTI)" About RTTI's later learning is more detailed. Finally, there is an important concept to reiterate: People often confuse the characteristics of those non-objective objects of Java, such as overloading of the method, often being introduced to everyone as an object-oriented feature. Millions Remember "Not binding, it is not polymorphism". (I also thought that overloading is an embodiment of polymorphism. Khan ~)

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

New Post(0)