JAVA SE Advanced Study Notes -03.Java -02- inheritance and polymorphism languages - Section 4 polymorphism

SUMMARY polymorphic .avi 1_4_4_15_

Here Insert Picture Description
1_4_4_16_ polymorphic .avi format using

Polymorphic implementation: references to the parent class subclass object

Here Insert Picture Description
1_4_4_17_ used in the polymorphic characteristics member variable .avi

The following is a description of priority access, then there is no upward (parent) to find

Two ways to access member variables methodology, direct access and indirect access member variables by means of variable

Polymorphism in, if the access method where the member variables (getter / setter), subclass overwrite the parent class method, the method belongs to the subclass, the final call is subclass of

Here Insert Picture Description
Here Insert Picture Description

Use Laid .avi 1_4_4_18_ members polymorphic method

For multi-state members access method

Compile look left, look to the right to run

Here Insert Picture Description
Here Insert Picture Description

Features access:
member variables
to compile the left to see the (parent), run to the left to see the (parent)
member methods
to compile the left to see the (parent), run to the right to see (subclass). Dynamic binding
static method
to compile the left to see the (parent), run to the left to see the (parent).
(Static and class-related, not really rewrite, so the visit is to the left of)
only non-static member method, the compiler look left, look to the right to run

1_4_4_19_ benefits of using polymorphic .avi

Here Insert Picture Description

Up 1_4_4_20_ object transformation .avi

Here Insert Picture Description

Downcast .avi 1_4_4_21_ object

Here Insert Picture Description
Here Insert Picture Description
Restore will appear as abnormal failure in the operation:

Exception in thread "main" java.lang.ClassCastException: com.zyk.opp.Zi cannot be cast to com.zyk.opp.Sonmethod Zi
MethodFu
10

Here there is a problem, downcast reduction, I do not know what the prototype is how to do?

About conversion of polymorphic types can look at the following discussion

Polymorphism in a few words can explain the JAVA intuitive about it?

1_4_4_22_ with instanceof keyword .avi

Use instanceof to solve the above problems encountered prototype

Down switch must be instanceof judgment, there would be ClassCastException abnormal

Here Insert Picture Description
Here Insert Picture Description
1_4_4_23_ laptop USB port analysis .avi case _

Here Insert Picture Description
1_4_4_24_ notebook case _ USB interface to achieve .avi

Here Insert Picture Description
Manual knock code to achieve, attention,
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Published 168 original articles · won praise 39 · views 110 000 +

Guess you like

Origin blog.csdn.net/u010481276/article/details/104592972
Recommended