Phase 1 Language Basics + Advanced _1-3-Java language senior _02- inheritance and polymorphism _ Section 4 polymorphism _17_ use features in a member variable polymorphism

Multi-state changes from the original variables, without any change

to create a parent class, define a member variable

to create a subclass inherits the parent class. Member variable num = 20 defines

the parent references to the subclass.

The left side of the equal sign to see who is

the output value of num is the parent of

a member variable can not be overwritten in

subclasses age = 16 to be a

parent is no age looking up Object, Object nor this age, this is obviously wrong the


indirect access to member variables by members method
methods of the parent class to create a showNum. Subclass inside nothing to write

access is a member of the parent class. Indirect member variable uses

this method belongs to whom. Who is using.


Or the value of num parent class output


if the subclass overwritten, showNum this method

then that value showNum subclass of output. Because subclasses have been overwritten, the equivalent of this method belongs to subclass








 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11220460.html