<> Misunderstanding

The first language to learn is English Java It's over , I still remember when I was a beginner Java When I was young , Still encountered a lot of difficulties , A lot of knowledge can not be understood . mention Java Object oriented programming , I believe many people will be impressed , But for beginners , It's really difficult to understand .

Remember when I didn't understand object-oriented polymorphism , I went to ask my sister , The answer is , It's understandable for the time being , Method overloading is a manifestation of polymorphism , As for deeper understanding , It can be understood over time .

Is method overloading a manifestation of polymorphism ? In fact, this is a misunderstanding , Let me understand wrong for a long time , Until another systematic review Java Only when I understand , Method overloading is not a manifestation of polymorphism .

The following is about overloading and polymorphism of methods .

<> Overload of method

heavy load : In the same class , More than one method with the same name is allowed , As long as their parameter number or parameter type is different .

<> characteristic

Independent of return value type , Just look at the parameter list , And the parameter list must be different . ( Number or type of parameters ). On call , It is distinguished according to the list of method parameters .

" Two identical different ":

*
Same class , Same method name

*
The parameter list is different : The number of parameters is different , Different parameter types

notes : Judge whether it is overloaded or not , The permission modifier associated with the method , return type , Parameter variable name , It doesn't matter !

<> Examples
// Returns the sum of two integers int add(int x,int y){return x+y;} // Returns the sum of three integers int add(int x,int y,int
z){return x+y+z;} // Returns the sum of two decimals double add(double x,double y){return x+y;}
<> Overload of method , Rewriting and polymorphism ( Method overloading is a manifestation of polymorphism ?NO)*

From the point of view of compiling and running :
heavy load , Multiple methods with the same name are allowed , The parameters of these methods are different . The compiler uses different parameter tables according to different methods , Modify the name of a method with the same name . For compilers , These methods with the same name become different methods
. Their call addresses are bound at compile time . Java Overload of can include parent and child classes , That is, subclasses can overload the methods of the same name and different parameters of the parent class .

therefore : For heavy loads , Before method call , The compiler has already determined the method to be called , This is called “ Early binding ” or “ Static binding ” ;

But for polymorphism , Only when the method is called , The interpreter runner will determine the specific method to be called , This is called “ Late binding ” or “ Dynamic binding ” .
Quote a sentence Bruce Eckel In my words : “ Don't be silly , If it's not late binding , It's not polymorphic .”

So , Method overloading is not a manifestation of polymorphism .

Let's take a look at polymorphism

<> Polymorphism

<> How to understand polymorphism ?

It can be understood as a variety of forms of a thing .

<> What is polymorphism ?

Polymorphism of objects : The reference of the parent class points to the object of the child class ( Or a reference assigned to a parent class by an object of a subclass )

* It can be directly applied to abstract classes and interfaces
<> The role of polymorphism

It improves the generality of the code , It is often called interface reuse

<> The use of polymorphism

Virtual method call

With the polymorphism of objects , We're at the compile time , Only methods declared in a parent class can be called , But during the operation period , What we are actually executing is the subclass overriding the parent class's method .

summary : compile , Look to the left ; function , Look to the right .

<> Precautions for polymorphism

Java There are two types of reference variables : Compile time type and run time type . The compile time type is determined by the type used to declare the variable , The runtime type is determined by the object actually assigned to the variable . abbreviation : Compile time ,
Look to the left ; Runtime , Look to the right .

*
If the compile time type is inconsistent with the run time type , The polymorphism of the object appears (Polymorphism)

*
In the case of polymorphism , “ Look to the left ” : Look at the reference of the parent class ( There is no subclass specific method in the parent class );“ Look to the right ” : We're looking at subclass objects ( What is actually running is the subclass overriding the parent class's method )

*
Polymorphism of objects — stay Java in , Subclass objects can be used instead of parent objects

*
A variable can only have one definite data type

*
A reference type variable may point to ( quote ) Many different types of objects
Person p = new Student(); Object o = new Person();//Object Variable of type o, point Person Object of type
o= new Student(); //Object Variable of type o, point Student Object of type
*
Subclasses can be regarded as special parents , So a reference to a parent type can point to an object of a child class : Upward transformation (upcasting).

*
If a reference type variable is declared as the type of the parent class , But the actual reference is a subclass object , Then the variable can no longer access the properties and methods added in the subclass
Student m = new Student(); m.school = “pku”; // legitimate ,Student There are two types school Member variable Person e
= new Student(); e.school = “pku”; // illegal ,Person Class does not school Member variable
Property is determined at compile time , Compile time e by Person type , No, school Member variable , Therefore, there is a compilation error .

*
Polymorphism of objects , Method only , Not applicable to attributes ( Compile and run on the left )

<> The premise of polymorphism

① Class inheritance or implementation relationship ② Method rewriting ③ Upward transformation

<> Examples of polymorphism

The formal parameter type declared by the method is the parent type , The method can be called with the object of the subclass as an argument
public class Test { public void method(Person e) { // …… e.getInfo(); } public
static void main(Stirng args[]) { Test t = new Test(); Student m = new Student()
; t.method(m); // Object of subclass m Parameters passed to the parent type e } }
<> Virtual method call (Virtual Method Invocation)

<> Normal method calls
Person e = new Person(); e.getInfo(); Student e = new Student(); e.getInfo();
<> Virtual method call ( In the case of polymorphism )

Methods with the same name and parameter as the parent class are defined in the subclass , In the case of polymorphism , The method of the parent class is called virtual method , A parent class is assigned to different subclass objects , Dynamically call the method that belongs to a subclass . Such a method call cannot be determined at compile time .
Person e = new Student(); e.getInfo(); // call Student Class getInfo() method
<> Compile time type and run time type

Compile time e by Person type , Method calls are determined at run time , So the call is Student Class getInfo() method . —— Dynamic binding

<> Diagram

<> premise

Person Class welcome() method , Subclasses override welcome().

<> implement :

In the case of polymorphism , Called object welcome() method , The actual implementation is the subclass override method .

Technology
©2019-2020 Toolsou All rights reserved,
tkinter Implementation of user login interface Vue Invalid render dynamic component in subcomponent ( First invalid , Valid for the second time )9 ride 9 Sudoku C/C++ Memory model TypeScript actual combat -12-TS The mechanism of type checking - Type inference Java Thread safety and insecurity JLink,STLink,DAPLink,CMSIS DAP Differences in use Chapter 10 use SpringMVC Frame transformation of supermarket order management system -1C Language calculation of deposit interest About the Blue Bridge Cup , Things you should know !