Table of contents
- 1. Dynamic binding mechanism
- Example A
- Example B
- Example C
- 2. Polymorphic array
- 3. Advanced usage of polymorphic arrays
- 4. Polymorphic parameters
- 5. Advanced usage of polymorphic parameters
1. Dynamic binding mechanism
Java's dynamic binding mechanism is very important
Example A
Let's look at an example:
Reading the above code, please explain what the following program will output:
The program will output 40 and 30. This example is very simple, just look at the running type. The running type of the code is B, so the method of class B will be called
Example B
Let's modify the above code and log out the following code block in the subclass:
Then the inheritance mechanism will access the sum method of the parent class:
So here is a question, will getI() here execute the subclass or the parent class?
When calling an object method, the method will be bound to the memory address/running type of the object
The running type of the code is still B, so the getI() method of the subclass will be executed here, and the result output is 30
Example C
Now let's modify the above code as follows
Then cancel the following code block in the subclass:
The inheritance mechanism will execute the sum1 method of the parent class:
So here is a question, will the i here use the subclass or the parent class?
There is no dynamic binding mechanism for attributes, where they are declared and where they are used (using the current class)
The i here is declared in the parent class, so the i attribute of the parent class will be selected, and the result is 20
2. Polymorphic array
definition:
The definition type of the array is the parent class type, but the actual element type saved is the subclass type
Person parent class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Student subclass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Teacher subclass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Test the use of polymorphic arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
output:
dahe 20
wangwei 11 100.0
zhangsan 12 60.0
wang 33 15000.0
li 55 25000.0
3. Advanced usage of polymorphic arrays
The Teacher subclass now has a new teaching method:
1 2 3 |
|
The student subclass has a new learning method:
1 2 3 |
|
So, is there a way to access the unique methods corresponding to their subclasses through polymorphic arrays? In fact, it can be solved by clever use of instanceof:
As a workaround, change the loop operation for polymorphic arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
output:
dahe 20
wangwei 11 100.0
students: wangwei is studying!
Zhangsan 12 60.0
Student: Zhangsan is learning!
Wang 33 15000.0
Teacher: Wang is giving a lecture!
li 55 25000.0
Teacher: li is giving a lecture!
You're done! Polymorphic arrays are powerful and perfect!
4. Polymorphic parameters
The formal parameter type of the method definition is the parent class type, and the actual parameter type is allowed to be the subclass type
Next, let's demonstrate the use of the following polymorphic parameters:
father:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Employee Subclass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Manager subclass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Let's test it and find the annual salary of employees in different positions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
output:
12000.0
62000.0
5. Advanced usage of polymorphic parameters
Let's improve the polymorphic parameter code above. If the input is an employee, call your own work method, and if the input is a manager, call your own manage method
Add the following method:
1 2 3 4 5 6 7 |
|
test:
1 2 |
|
output:
Ordinary employees: Mr. Zhang is working!
Manager: milan is managing!