Java deep understanding polymorphism

Polymorphism

1) Meaning:

The same action effect of different behaviors for different objects. eg: F1

2) Advantages:

Reducing the coupling between programs
enhance the scalability of the program

3) the type of polymorphism:

---- polymorphism compilation: static, method overloading, (at compile time, calls the same method to distinguish between different parameter)
---- runtime polymorphism: dynamic, can only be determined during runtime Object type used, (a reference to determine the variable
is the instance)

4) of polymorphic necessary condition

---- inheritance: the presence of the subclass, the parent class hierarchy
---- Re: redefine subclasses of the parent class (covering method)
---- upcast: Person instance of the parent class reference point subclass p = new Man ();

5 reflects the embodiment) polymorphism

---- Based reflect the way inheritance
-----based inheritance reflects the way

Polymorphic 6) object

---- see the equal sign left at compile time, see the right execution (see compile-time parent, call the child class method is executed)

7) and equals the difference == Method:

1) the basic data types, = comparison value =
2) reference data type, address comparator = =
3) equals Objet class method is a method of implementation is to use his underlying comparing = =
4) can override all classes equals ( ) method, eg: string class override equals () is to compare strings have the same contents

Reflecting patterns 8) polymorphism

1) inheritance-based implementations
---- different subclasses override different methods embodied in the form of the parent class
2) based implementation of the interface
transformation and lower transition 3.
1) the transition: the subclass object to parent class reference, can be automatically converted
2) transformation: conversion casts

9) final keyword

1) the modified class can not be inherited
2) modified by members of the attribute is a constant, can not change the
naming ---- constants, all caps, if multiple words, separated by underscores
in the constant storage method ---- constant pool area
3) the modified method can not be rewritten

10) static keyword

1) modified member variable
---- a class variable
---- With the loaded class produce, no matter how many objects are created in the future, class variables are created only once (keep a copy)
---- accessing static properties: the class name attribute name
---- all objects can share static member property
---- stored in the method area
2) modification member methods
---- is a class method
---- accessing static methods: class name method name
---- stored in the method area
---- static method in static members can only access (if the access non-static members can only be accessed by instantiating objects)
---- tools in general the method will be declared as static method
3) static block
---- syntax: {} static
---- as class loading and loading, only once

11) variable parameters

1) Syntax: ... variable data type parameter name
2) can take any parameters (zero or more)
3) Common Parameters If both the parameter list, but also a variable parameter, the variable parameter in a certain final statement

12) type of conversion

1) The basic data types into
---- automatic type conversions: The conversion of a small capacity type data capacity of the data type (data type similar)
. IB (byte, char) -> 2B (Short, Boolean) -> 4B (int, float) ->. 8B (Long, Double)
---- mandatory conversion: converting capacity of the data type is the type of small-capacity data (similar data types)
can lead to loss of precision.
2) reference data types into
---- upward transition (automatic type conversions): parent class objects pointing object subclass. P = Man new new the Person ();
---- downcast: mandatory type conversion, man = Man (Man) P;
---- two classes if nothing, it can not be transformed. Man man = new Woman (); // error
---- instanceof: for comparison type

Published 15 original articles · won praise 16 · views 727

Guess you like

Origin blog.csdn.net/achu19/article/details/103978112