1. Polymorphic mechanism is one of the essence of object-oriented technology, it is based on inheritance. The so-called polymorphism (polymorphism) can be understood as a variety of existence forms of a certain kind of thing.
Polymorphism means that a method with the same name may have multiple versions in the program, the user uses the same method name to invoke the method, the system will invoke the different version of the method according to the specific situation, so as to achieve different functions, and implement "one interface, multiple methods."
Polymorphism allows the processing of existing variables and related classes in a unified style, making it easy to add new functionality to the system, and inheritance and polymorphism are effective technologies for reducing software complexity; polymorphism technology mainly manifests in the method overload and the method covers two aspects. The inherited permissions do not change.
2. The parent class reference points to the child class object. Objects of subclasses can be used in place of objects of the parent class.
In the inheritance of a class, subclasses are expanded and transformed on the basis of the parent class, the parent class has a member subclass that can be considered a strong function of the parent class, or the object of the subclass should be more powerful than the object of the parent class, so that the object of the subclass should be able to override the object of the parent class.
3.1). Manifestation of polymorphism:
the reference to the parent class points to its own subclass object;
a reference to a parent class can receive its own subclass object.
2. Polymorphism of the premise: to have an inheritance relationship, there is coverage.
3. Polymorphism Benefits: Increases program extensibility.
disadvantage: You can only use the parent class reference to access members in the parent class.
if you want to access a specific method of a subclass, force the reference of the parent class to the subclass type.
4.1). Features of member methods in polymorphic states: Compile to look to the left, run to the right. (at compile time, see if the class of the reference type has this method, and at run time, see if the object's class has a method called.) ) (Dynamic binding)
2. The characteristics of member variables in polymorphism: Compile and run all look to the left.
5. Instanceof operator (instance is an example of the meaning, that is, an instance)
operator instanceof is used to detect the true type of an object.
Μ format:< variable name > instanceof < type >
Μ function: If the variable on the left side of the instanceof operator is the true type of the object being referenced at the moment, or its subclass, the entire expression evaluates to true, otherwise the result is false.
If the argument is an object of the subclass student when the method is invoked, the shape is correct and the shape fails if the argument is an object of the parent class person.
if (P instanceof Student) {
studentst= (Student) p;
St.display ();
}
6. As with the mandatory type conversion (downward transition) between the base data types, there is a reciprocal conversion between the parent and subclass objects of the inheritance relationship (that is, the conversion of the reference type), adhering to the following guidelines:
1. A subclass object can be treated as an object of its parent class.
2. The parent class object cannot be treated as an object of a subclass.
3. If the formal parameter of a method defines the object of the parent class, the subclass object can be used as the actual argument when the method is called.
4. If the parent object and reference point to a subclass object, the reference to the parent class object can be cast as a reference to the subclass object. The format is: (subclass name) parent class name.
For example: Person p = newstudent (); Upward transformation, class of Ascension.
Student s = (Student) p;//downward transition, if p must point to Student (that is, the previous statement), and the error when pointing to person.
7. The initialization block is the fourth member that can appear in the Java class (the first three are: property, member method, constructor, a class can have more than one initialization block, between the same type of initialization block, as well as initialization blocks and declaring instance properties when the initial value specified is the initialization code for that instance. The order of execution is the same as the order in which they are arranged in the source program. The format is:
Class Name {
[modifier]{//modifiers can only be static or nothing (nothing is known as a constructor initialization block or directly called an initialization block).
//initialization block executable code
}
8. When a Java object (that is, a new object) is created, the system always invokes the initialization block defined in the class;
Μ If two ordinary initialization blocks are defined in a class, the previously defined initialization block executes first, followed by the initialization block defined later.
Μ initialization block is also a member of a Java class, but it has no name, there is no identity, so the class, object can not invoke the initialization block;
Μ initialization blocks can only be executed automatically when an object is created and before the constructor method is executed. The new object does not invoke the initialization block, as long as new is automatically invoked, the new one is tuned, and the new multiple is called back.
Call Order: Initialize block-> construct method-> common method.
9. Different initialization blocks and construction methods: the initialization block is a fixed code of execution, and it cannot accept any arguments. The initialization block is therefore identical to the initialization of the properties within the same class.
usage: If more than one constructor has the same initialization code, and the code does not accept parameters, it can be defined in the initialization block. It can better improve the reuse of initialization block and improve the maintainability of the whole application.
When a Java object is created, not only does the initialization block and construction method of the class execute, the system executes the initialization block and the construction method of its parent class first. Similar to the construction method, when a Java object is created, not only does the initialization block and construction method of the class execute, it also goes back to the Java.lang.Object class, executes the initialization block and construction method of the object class, and then executes the initialization block construction method of the parent class in descending order .... Finally, the initialization block and the construction method of the class are executed, and the object of the class is returned.
10. Static initialization block (when class is loaded and executed only once)
If the static modifier is used when defining the initialization block, the initialization block becomes a static initialization block, also known as a class initialization block.
Μ Static initialization blocks are class-related, and the system performs static initialization blocks in the class initialization phase, rather than when the object is created, so the static initial block is always executed before the normal initialization block.
The Μ static initial block belongs to the static members of the class, which is used to initialize the class property and cannot initialize the instance property. Also follow rules in which static members cannot access non-static members.
The Μ system performs static initialization in the class initialization phase, not only the static initialization block of this class, but also the object class (if it contains a static initialization block). After this process, the initialization process of the class is completed. (The parent class is statically initialized block, then the static initialization block of the tone class, then the normal initialization block and the construction method of the parent class, and then the subclass ...) )
Note: Both the static initial block and the initial value specified when declaring the static property are the initialization code for the class, and they are executed in the same order as the source program